+ Start a Discussion
sales@myvarmasales@myvarma 

email using apex and vf page help me bros

i m trying to send mail to using apex and vf page
help me


public class emailin_class
{
 public string subject_c{get; set;}
    public string email_body_c{get; set;}
    public string toAddresses{get; set;}  
list<string> emails = new list<string>();
 
 
 public PageReference send()
 {
      
 messaging.singleEmailmessage email=new messaging.singleEmailmessage();
 
 email.setSubject(subject_c);
 email.setPlainTextBody(email_body_c);

     email.setToAddresses(toAddresses);
   
 messaging.sendEmailResult[] r=messaging.sendEmail(new messaging.singleEmailmessage[] {email});
 
 
  return null;
 }

 }
 
<apex:page controller="emailin_class">
<apex:form >
    <apex:pageblock title="Email Messaging">
        <apex:pageblocksection columns="1">
    <b> Enter Email Subject:   </b>  
        <apex:inputtext value="{!subject_c}" maxlength="100"/>
       
       <!--  <b> Enter email_body: </b>
        <apex:inputTextarea value="{!email_body_c}" rows="6" cols="50" id="email_body"/>
         -->
        <apex:outputLabel for="email_body_c" value="Enter Body Here"/>
        <apex:inputtextarea value="{!email_body_c}" rows="6" cols="50" id="email_body_c"/> 
        
        
        <b>Enter Email ToRecipients:   </b>  
        <apex:inputtext value="{!toAddresses_c}" maxlength="50"/>
            
        </apex:pageblocksection>
       <apex:pageBlockButtons location="top">
           <apex:commandbutton value="Send Emails Now" action="{!send}"/>   
        </apex:pageBlockButtons>
           
    </apex:pageblock>
    
    </apex:form>

</apex:page>

 
Best Answer chosen by sales@myvarma
Amit Singh 1Amit Singh 1
Hi,

Use below code.
-- VF --
<apex:page controller="emailin_class">
<apex:form >
    <apex:slds/>
    <apex:pageblock title="Email Messaging">
        <apex:pageblocksection columns="1">
    <b> Enter Email Subject:   </b>  
        <apex:inputtext value="{!subject_c}" maxlength="100"/>
       
       <!--  <b> Enter email_body: </b>
        <apex:inputTextarea value="{!email_body_c}" rows="6" cols="50" id="email_body"/>
         -->
        <apex:outputLabel for="email_body_c" value="Enter Body Here"/>
        <apex:inputtextarea value="{!email_body_c}" rows="6" cols="50" id="email_body_c"/> 
        
        
        <b>Enter Email ToRecipients:   </b>  
        <apex:inputtext value="{!toAddresses}" maxlength="50"/>
            
        </apex:pageblocksection>
       <apex:pageBlockButtons location="top">
           <apex:commandbutton value="Send Emails Now" action="{!send}"/>   
        </apex:pageBlockButtons>
           
    </apex:pageblock>
    
    </apex:form>

</apex:page>
-- Controller --
public class emailin_class
{
 public string subject_c{get; set;}
    public string email_body_c{get; set;}
    public string toAddresses {get; set;}  
    list<string> emails = new list<string>();
 
 
 public PageReference send()
 {
      
 messaging.singleEmailmessage email=new messaging.singleEmailmessage();
 
 email.setSubject(subject_c);
 email.setPlainTextBody(email_body_c);

     email.setToAddresses(new List<String>{toAddresses});
   
 messaging.sendEmailResult[] r=messaging.sendEmail(new messaging.singleEmailmessage[] {email});
 
 
  return null;
 }

 }
Let me know the outcome.
Thanks,
Amit Singh

All Answers

Amit Singh 1Amit Singh 1
Hi,

Use below code.
-- VF --
<apex:page controller="emailin_class">
<apex:form >
    <apex:slds/>
    <apex:pageblock title="Email Messaging">
        <apex:pageblocksection columns="1">
    <b> Enter Email Subject:   </b>  
        <apex:inputtext value="{!subject_c}" maxlength="100"/>
       
       <!--  <b> Enter email_body: </b>
        <apex:inputTextarea value="{!email_body_c}" rows="6" cols="50" id="email_body"/>
         -->
        <apex:outputLabel for="email_body_c" value="Enter Body Here"/>
        <apex:inputtextarea value="{!email_body_c}" rows="6" cols="50" id="email_body_c"/> 
        
        
        <b>Enter Email ToRecipients:   </b>  
        <apex:inputtext value="{!toAddresses}" maxlength="50"/>
            
        </apex:pageblocksection>
       <apex:pageBlockButtons location="top">
           <apex:commandbutton value="Send Emails Now" action="{!send}"/>   
        </apex:pageBlockButtons>
           
    </apex:pageblock>
    
    </apex:form>

</apex:page>
-- Controller --
public class emailin_class
{
 public string subject_c{get; set;}
    public string email_body_c{get; set;}
    public string toAddresses {get; set;}  
    list<string> emails = new list<string>();
 
 
 public PageReference send()
 {
      
 messaging.singleEmailmessage email=new messaging.singleEmailmessage();
 
 email.setSubject(subject_c);
 email.setPlainTextBody(email_body_c);

     email.setToAddresses(new List<String>{toAddresses});
   
 messaging.sendEmailResult[] r=messaging.sendEmail(new messaging.singleEmailmessage[] {email});
 
 
  return null;
 }

 }
Let me know the outcome.
Thanks,
Amit Singh
This was selected as the best answer
sales@myvarmasales@myvarma
errors:


System.EmailException: SendEmail failed. First exception on row 0; first error: INVALID_EMAIL_ADDRESS, Email address is invalid: pradeep: [toAddresses, pradeep]
Error is in expression '{!send}' in component <apex:commandButton> in page myemailmessaging: Class.emailin_class.send: line 19, column 1
Class.emailin_class.send: line 19, column 1
sales@myvarmasales@myvarma
got it bro input mistake