Wednesday, November 30, 2016

Sending emails by using SingleEmailMessage

Sending emails: By using Single email message we can send emails.
SingleEmailMessage class contains methods for sending emails. SingleEmailMessage contains below methods.

1. setBccAddresses(String[])
2. setCcAddresses(String[])
3. setCharset(String)
4. setDocumentAttachments(ID[])
5. setFileAttachments(EmailFileAttachment[])
6. setHtmlBody(String)
7. setInReplyTo(String)
8. setPlainTextBody(String)
9. setOrgWideEmailAddressId(ID)
10. setReferences(String)
11. setSubject(String)
12. setTargetObjectId(ID)
13. setToAddresses(String[])
14. setWhatId(ID)

Example Apex Class: Below class is a simple apex program to understand single
email message. I have created vf page with button. & when we click on that button
that calls sending emails method in below class. Try this for sending emails

Page:


<apex:page controller="singleEmailExample">
<apex:form >
             <apex:commandButton value="SendEmail" action="{!sendingEmail}"/>
</apex:form>
</apex:page>

Controller:

public class singleEmailExample{
        public PageReference sendingEmail(){
               Messaging.SingleEmailMessage semail = new Messaging.SingleEmailMessage();
               String[] sendingTo = new String[]{'XXXXXXXXXXXXXX@gmail.com'};
               semail.setToAddresses(sendingTo);
               String[] sendingToBccAdd = new String[]{'XXXXXXXXX@gmail.com'};
               semail.setBccAddresses(sendingToBccAdd);
               String[] sendingTocAdd = new String[]{'XXXXXXXXXXX@gmail.com'};
               semail.setCcAddresses(sendingTocAdd);
               semail.setSubject('Single Email message Example');
               semail.setPlainTextBody('Hello!!!!!!!!!!This is a test email to test single email message program');
              Messaging.sendEmail(new Messaging.SingleEmailMessage[] {semail});
              return null;
      }
}

No comments:

Post a Comment