SingleEmailMessage extends Email and inherits all of its methods. 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. However, the getTemplateName() method doesn’t have an equivalent setter method; use setTemplateId() to specify a template name.
The following are constructors for SingleEmailMessage.
The following are methods for SingleEmailMessage. All are instance methods. All base email (Email class) methods are also available to the SingleEmailMessage objects. These methods are described in Email Class (Base Email Methods).
public STRING getTemplateName()
Type: String
There is no equivalent setter method for getTemplateName(). If the email didn’t use a template, getTemplateName() returns nothing. If you use setTemplateId(), and then call getTemplateName(), the template name associated to the template ID is returned.
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 setTemplateId(ID templateId)
Type: Void
public Void setToAddresses(String[] toAddresses)
Type: Void
public void setTreatBodiesAsTemplate(Boolean treatAsTemplate)
Type: void
public void setTreatTargetObjectAsRecipient(Boolean treatAsRecipient)
Type: void
You can set TO, CC, and BCC addresses using the email messaging methods regardless of whether a template is used for the email or the target object is a recipient.
public Void setWhatId(ID whatId)
Type: Void
The value must be one of the following types: