In this guide, we will walk through the technical details of using Messaging.SingleEmailMessage to send emails from Apex code.
First, let's take a look at the basic structure of the SingleEmailMessage class. The class has several properties that you can set to customize your email, including:
ToAddresses: A list of email addresses to send the email to.
CCAddresses: A list of email addresses to include as carbon copies.
BCCAddresses: A list of email addresses to include as blind carbon copies.
Subject: The subject of the email.
PlainTextBody: The plain text body of the email.
HTMLBody: The HTML body of the email.
ReplyTo: The email address to use as the reply-to address.
FileAttachments: A list of attachments to include in the email.
Here are some of the commonly used methods of the SingleEmailMessage class along with their usage:
setToAddresses(String[] addresses): This method is used to set the recipient email addresses of the email.
setSubject(String subject): This method is used to set the subject of the email.
setPlainTextBody(String body): This method is used to set the plain text body of the email.
setHtmlBody(String body): This method is used to set the HTML body of the email.
setReplyTo(String email): This method is used to set the reply-to email address for the email.
setSenderDisplayName(String name): This method is used to set the display name of the sender.
setTargetObjectId(Id targetObjectId): This method is used to set the ID of the related object for the email.
setWhatId(Id whatId): This method is used to set the ID of the parent object for the email.
setSaveAsActivity(Boolean saveAsActivity): This method is used to indicate whether the email should be saved as a task in Salesforce.
addAttachment(Attachment attachment): This method is used to add an attachment to the email.
setFileAttachments(List<Messaging.EmailFileAttachment> attachments): This method is used to set the file attachments for the email.
To use the SingleEmailMessage class, you will first need to create a new instance of the class and set the properties that you want to use. For example, to send an email to a single recipient with a plain text body, you would use the following code:
email.setSaveAsActivity(true);email = new Messaging.SingleEmailMessage();
email.setToAddresses(newString[] {'recipient@example.com'});
email.setSubject('Test Email');
emeail.setPlainTextBody('Hello, this is a test email.');
email.setReplyTo('replyto@example.com');
email.setSenderDisplayName('Sender Name');
email.setTargetObjectId(targetObjectId);
email.setWhatId(whatId);
Once you have set the properties of the email, you can use the Messaging.sendEmail method to send the email. This method takes a list of SingleEmailMessage objects as its parameter, so you can send multiple emails at once if desired. For example, to send the email we created earlier, you would use the following code:
Messaging.sendEmail(newMessaging.SingleEmailMessage[] {email});
It is also possible to include attachments to the email by using the addFileAttachment() method of the SingleEmailMessage class. For example, to include a PDF file as an attachment, you would use the following code:
Messaging.SingleEmailMessageemail=newMessaging.SingleEmailMessage(); email.setToAddresses(newString[] {'recipient@example.com'}); email.setSubject('Test Email with Attachment'); email.setPlainTextBody('Hello, this is a test email with attachment.');
Messaging.EmailFileAttachmentpdfAttachment = newMessaging.EmailFileAttachment();
pdfAttachment.setFileName('test.pdf');
pdfAttachment.setBody(pdfData); pdfAttachment.setContentType('application/pdf'); email.addFileAttachment(pdfAttachment);
Messaging.sendEmail(newMessaging.SingleEmailMessage[] {email});
Additionally, we can save the email as a task by using the setSaveAsActivity method and passing true as a parameter.
mail.setSaveAsActivity(true);
That's it! With just a few lines of code, you can send emails from Salesforce Apex code using the powe class. Whether you need to send a simple message to one recipient or a more complex message with attachments, this class provides all the tools you need to get the job done.