In Salesforce, sending emails using Apex is a powerful feature enabled by the SingleEmailMessage class. This class allows developers to customize the email content and delivery, making it versatile for business communications. Some key methods include setToAddresses()
for specifying recipients, setCcAddresses()
and setBccAddresses()
for adding copies to others, and setSubject()
for defining the subject line. Additionally, both plain text and HTML formats can be used via setPlainTextBody()
and setHtmlBody()
. This flexibility ensures that emails are not only informative but also professionally formatted for different use cases.
Furthermore, attachments can be sent using setFileAttachments()
or setDocumentAttachments()
, making this feature ideal for sending invoices, contracts, or other files. A practical example is provided through an Apex class and Visualforce page, where clicking a button on the page triggers an email with the configured properties. This showcases how businesses can automate personalized communications using Salesforce’s native tools, enhancing operational efficiency and engagement with clients.
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.
Here’s a table explaining the methods in the SingleEmailMessage class for sending emails in Salesforce:
Method Name | Description |
---|---|
setBccAddresses(String[]) | Sets blind carbon copy (BCC) recipients for the email. Recipients in this list won’t be visible to others. |
setCcAddresses(String[]) | Sets carbon copy (CC) recipients for the email. These recipients will see the primary and CC recipients. |
setCharset(String) | Defines the character set (e.g., UTF-8) used to encode the email, ensuring proper display of text. |
setDocumentAttachments(ID[]) | Attaches Salesforce documents (by their ID) to the email. Useful for sending internal documents. |
setFileAttachments(EmailFileAttachment[]) | Attaches external files (from the local system or external source) to the email. |
setHtmlBody(String) | Sets the HTML content of the email. This method allows for rich text formatting and visuals. |
setInReplyTo(String) | Links the email to another message by setting the “In-Reply-To” header, useful in email threads. |
setPlainTextBody(String) | Sets the plain text content of the email. This is typically used for simple, text-only emails. |
setOrgWideEmailAddressId(ID) | Specifies an org-wide email address for the sender, ensuring consistent sending identities. |
setReferences(String) | Adds reference information to the email, often used for linking email threads. |
setSubject(String) | Sets the subject of the email. This defines what appears in the email’s subject line. |
setTargetObjectId(ID) | Associates the email with a specific Salesforce record (like a contact or lead) by its ID. |
setToAddresses(String[]) | Sets the primary recipients of the email. Recipients listed here will see each other. |
setWhatId(ID) | Associates the email with a related Salesforce object (like an Opportunity or Case) by its ID. |
This table outlines how each method customizes different aspects of an email, helping developers automate and personalize emails within Salesforce.
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
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; } }
Apex Page
<apex:page controller="singleEmailExample"> <apex:form > <apex:commandButton value="SendEmail" action="{!sendingEmail}"/> </apex:form> </apex:page>