All base email (Email class) methods are also available to the SingleEmailMessage objects.
Email properties are readable and writable. Each property has corresponding setter and getter methods. For example, the toAddresses() property is equivalent to the setToAddresses() and getToAddresses() methods. Only the setter methods are documented.
The following are methods for SingleEmailMessage. All are instance methods.
public Void setBccAddresses(String[] bccAddresses)
Type: Void
If the BCC compliance option is set at the organization level, the user cannot add BCC addresses on standard messages. The following error code is returned: BCC_NOT_ALLOWED_IF_BCC_ COMPLIANCE_ENABLED. Contact your Salesforce representative for information on BCC compliance.
public Void setCcAddresses(String[] ccAddresses)
Type: Void
public Void setCharset(String characterSet)
Type: Void
public Void setDocumentAttachments(ID[] documentIds)
Type: Void
You can attach multiple documents as long as the total size of all attachments does not exceed 10 MB.
public Void setFileAttachments(EmailFileAttachment[] fileNames)
Type: Void
You can attach multiple files as long as the total size of all attachments does not exceed 10 MB.
public Void setHtmlBody(String htmlBody)
Type: Void
public Void setInReplyTo(String parentMessageIds)
Type: Void
public void setOptOutPolicy(String emailOptOutPolicy)
Type: void
This example shows how to send an email with the opt-out setting enforced. Recipients are specified by their IDs. The FILTER option causes the email to be sent only to recipients that haven’t opted out from email. This example uses dot notation of the email properties, which is equivalent to using the set methods.
Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage(); // Set recipients to two contact IDs. // Replace IDs with valid record IDs in your org. message.toAddresses = new String[] { '003D000000QDexS', '003D000000QDfW5' }; message.optOutPolicy = 'FILTER'; message.subject = 'Opt Out Test Message'; message.plainTextBody = 'This is the message body.'; Messaging.SingleEmailMessage[] messages = new List<Messaging.SingleEmailMessage> {message}; Messaging.SendEmailResult[] results = Messaging.sendEmail(messages); if (results[0].success) { System.debug('The email was sent successfully.'); } else { System.debug('The email failed to send: ' + results[0].errors[0].message); }
public Void setPlainTextBody(String plainTextBody)
Type: Void
public Void setOrgWideEmailAddressId(ID emailAddressId)
Type: Void
public Void setReferences(String references)
Type: Void
public Void setSubject(String subject)
Type: Void
public Void setTargetObjectId(ID targetObjectId)
Type: Void
public Void setToAddresses(String[] toAddresses)
Type: Void
public void setTreatBodiesAsTemplate(Boolean treatAsTemplate)
Type: void
public void setTreatTargetObjectAsRecipient(Boolean treatAsRecipient)
Type: void
public Void setWhatId(ID whatId)
Type: Void
The value must be one of the following types: