Monday, September 12, 2016

Outbound Messaging :: Send Email using apex class and Visualforce page.

                     Outbound Messaging


Send email using apex class and Visualforce page:

1. Create apex controller

public class EmailWithVisualforce{
   public string subject {get;set;}
   public string Body{get;set;}
   public string EmailAddress{get;set;}
   list<string> emails = new list<string>();
 
  public pageReference SendEmail(){
     Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
     //Split multiple email addess by , seperated.
     emails.addAll(EmailAddress.split(','));
     email.setSubject( subject );
     email.setPlainTextBody( Body);
     email.setToAddresses( emails );
     Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
     return null;
  }
}

2. Create Visualforce Page:

<apex:page controller="EmailWithVisualforce">
   <apex:form >
      <apex:pageBlock title="Email With Visulforce Page">
          <apex:pageBlockSection columns="1">
             <apex:pageBlockSectionItem >
                 <apex:outputLabel value="E-Mail:" for="edid"/>
                 <apex:inputtext value="{!EmailAddress}" id="edid"/>
             </apex:pageBlockSectionItem>
             <apex:pageblockSectionItem >
                 <apex:outputLabel value="Subject : " for="subject"/>
                 <apex:inputtext value="{!subject }" id="subject"/>
             </apex:pageblockSectionItem>
           
             <apex:pageblockSectionItem >
                <apex:outputlabel value="Enter Message Here:" for="Body"/>
                <apex:inputtextarea value="{!Body}" rows="8" cols="80" id="Body" style="color:red" />
             </apex:pageblockSectionItem>
          </apex:pageBlockSection>
     
        <apex:pageBlockButtons >
           <apex:commandButton value="Send !" action="{!SendEmail}"/>
        </apex:pageBlockButtons>
       
      </apex:pageBlock>
   </apex:form>
</apex:page>

You can see below UI once you done the step 1 & 2:





No comments:

Post a Comment

Show message when server is busy.

Please Wait...