Messaging Class
Contains messaging methods used when sending a single or mass email.
Sending email by using Apex requires domain-level and user-level email verification. System-generated emails also require verification of the From email address. Email delivery fails if any of these verifications is incomplete. See Requirements to Send Email from Salesforce.
The following are methods for Messaging. All are instance methods.
-
extractInboundEmail(source, includeForwardedAttachments)
Use this method in your email service code to control how to parse and process forwarded or attached emails. Returns an instance of
Messaging.InboundEmailfrom a stream of data that is in RFC822 format. The data stream can be a forwarded email in an attachment to an existing InboundEmail, or a stream from another source. -
reserveMassEmailCapacity(amountReserved)
Reserves email capacity to send mass email to the specified number of email addresses, after the current transaction commits.
-
reserveSingleEmailCapacity(amountReserved)
Reserves email capacity to send single email to the specified number of email addresses, after the current transaction commits.
-
sendEmail(emails, allOrNothing)
Sends the list of emails instantiated with either
SingleEmailMessageorMassEmailMessageand returns a list of SendEmailResult objects. When org preferences are set to save EmailMessage objects and a trigger is defined for EmailMessage objects, the trigger is fired for eachSingleEmailMessageindividually. ThesendEmailmethod can be called 10 times per Apex transaction and each method invocation can include up to 100 “To”, 25 “Cc”, and 25 “Bcc” recipients. -
sendEmailMessage(emailMessageIds, allOrNothing)
Sends draft email messages as defined by the specified email message IDs and returns a list of SendEmailResult objects.
-
renderEmailTemplate(whoId, whatId, bodies)
Replaces merge fields in text bodies of email templates with values from Salesforce records. Returns an array of
RenderEmailTemplateBodyResultobjects, each of which corresponds to an element in the supplied array of text bodies. EachRenderEmailTemplateBodyResultprovides a success or failure indication, along with either an error code or the rendered text. -
renderStoredEmailTemplate(templateId, whoId, whatId)
Renders a text, custom, HTML, or Visualforce email template that exists in the database into an instance of
Messaging.SingleEmailMessage. Includes all attachment content in the returned email message. -
renderStoredEmailTemplate(templateId, whoId, whatId, attachmentRetrievalOption)
Renders a text, custom, HTML, or Visualforce email template that exists in the database into an instance of
Messaging.SingleEmailMessage. Provides options for including attachment metadata only, attachment metadata and content, or excluding attachments. -
Renders a text, custom, HTML, or Visualforce email template that exists in the database into an instance of
Messaging.SingleEmailMessage. Provides options for including attachment metadata only, attachment metadata and content, or excluding attachments.
Use this method in your email service code to control how to parse and process forwarded or attached emails. Returns an instance of Messaging.InboundEmail from a stream of data that is in RFC822 format. The data stream can be a forwarded email in an attachment to an existing InboundEmail, or a stream from another source.
public static Messaging.InboundEmail extractInboundEmail(Object source, Boolean includeForwardedAttachments)
-
source:
Type:
Object An instance of
Messaging.InboundEmail.BinaryAttachmentwhose MimeTypeSubtype ismessage/rfc822or a Blob. If source is a Blob, then supply a byte array in RFC822 format. -
includeForwardedAttachments:
Type: Boolean
This parameter controls how attachments to embedded or forwarded emails are handled. Set to
trueto provide all attachments, even attachments in embedded emails in thebinaryAttachmentsandtextAttachmentsproperties of the returned value. Set tofalseto provide only the attachments that are at the top level of the source email.
Type: Messaging.InboundEmail
Reserves email capacity to send mass email to the specified number of email addresses, after the current transaction commits.
public Void reserveMassEmailCapacity(Integer amountReserved)
-
amountReserved:
Type: Integer
Type: Void
This method can be called when you know in advance how many addresses emails will be sent to as a result of the transaction. If the transaction would cause the organization to exceed its daily email limit, using this method results in the following error: System.HandledException: The daily limit for the org would be exceeded by this request.If the organization doesn’t have permission to send API or mass email, using this method results in the following error: System.NoAccessException: The organization is not permitted to send email.
Reserves email capacity to send single email to the specified number of email addresses, after the current transaction commits.
public Void reserveSingleEmailCapacity(Integer amountReserved)
-
amountReserved:
Type: Integer
Type: Void
This method can be called when you know in advance how many addresses emails will be sent to as a result of the transaction. If the transaction would cause the organization to exceed its daily email limit, using this method results in the following error: System.HandledException: The daily limit for the org would be exceeded by this request.If the organization doesn’t have permission to send API or mass email, using this method results in the following error: System.NoAccessException: The organization is not permitted to send email.
Sends the list of emails instantiated with either SingleEmailMessage or MassEmailMessage and returns a list of SendEmailResult objects. When org preferences are set to save EmailMessage objects and a trigger is defined for EmailMessage objects, the trigger is fired for each SingleEmailMessage individually. The sendEmail method can be called 10 times per Apex transaction and each method invocation can include up to 100 “To”, 25 “Cc”, and 25 “Bcc” recipients.
public Messaging.SendEmailResult[] sendEmail(Messaging.Email[] emails, Boolean allOrNothing)
-
emails:
Type: Messaging.Email[]
-
allOrNothing:
Type: Boolean
The optional opt_allOrNone parameter specifies whether
sendEmailprevents delivery of all other messages when any of the messages fail due to an error (true), or whether it allows delivery of the messages that don’t have errors (false). The default istrue.
Type: Messaging.SendEmailResult[]
Sends draft email messages as defined by the specified email message IDs and returns a list of SendEmailResult objects.
public Messaging.SendEmailResult[] sendEmailMessage(List<ID> emailMessageIds, Boolean allOrNothing)
Type: Messaging.SendEmailResult[]
If the emailMessageIds parameter is null, the method throws a System.IllegalArgumentException exception.
The sendEmailMessage method assumes that the optional allOrNothing parameter is always false and ignores the value you set. Delivery of all messages is attempted even if some messages fail due to an error.
The email address of the user calling the sendEmailMessage method is inserted in the From Address field of the email header and the Email Message record.
This example shows how to send a draft email message. It creates a case and a new email message associated with the case. Next, the example sends a draft email message and checks the results. Before running this example, make sure to replace the email address with a valid address.
In API version 54.0 and later, a null emailMessageIds parameter results in a System.IllegalArgumentException exception. In API version 53.0 and earlier, a null emailMessageIds parameter results in an error.
Replaces merge fields in text bodies of email templates with values from Salesforce records. Returns an array of RenderEmailTemplateBodyResult objects, each of which corresponds to an element in the supplied array of text bodies. Each RenderEmailTemplateBodyResult provides a success or failure indication, along with either an error code or the rendered text.
public static List<Messaging.RenderEmailTemplateBodyResult> renderEmailTemplate(String whoId, String whatId, List<String> bodies)
-
whoId:
Type: String
The identifier of an object in the database, typically a contact, lead, or user. The database record for that object is read and used in merge field processing.
-
whatId:
Type: String
Identifies
an object in the database like an account or opportunity. The record for that object is read and used in merge field processing.
-
bodies:
Type: List<String>
An array of strings that are examined for merge field references. The corresponding data from the object referenced by the
whoIdorwhatIdreplaces the merge field reference.
Type: List<Messaging.RenderEmailTemplateBodyResult>
Use this method in situations in which you want to dynamically compose blocks of text that are enriched with data from the database. You can then use the the rendered blocks of text to compose and send an email or update a text value in another database record.
Executing the renderEmailTemplate method counts toward the SOQL governor limit. The number of SOQL queries that this method consumes is the number of elements in the list of strings passed in the bodies parameter.
See Also
Renders a text, custom, HTML, or Visualforce email template that exists in the database into an instance of Messaging.SingleEmailMessage. Includes all attachment content in the returned email message.
public static Messaging.SingleEmailMessage renderStoredEmailTemplate(String templateId, String whoId, String whatId)
-
templateId:
Type: String
An email template that exists in the database, such as text, HTML, custom, and Visualforce templates.
-
whoId:
Type: String
The identifier of an object in the database, typically a contact, lead, or user. The database record for that object is read and used in merge field processing.
-
whatId:
Type: String
Identifies
an object in the database, like an account or opportunity. The record for that object is read and used in merge field processing.
Type: Messaging.SingleEmailMessage
Executing the renderStoredEmailTemplate method counts toward the SOQL governor limit as one query.
See Also
Renders a text, custom, HTML, or Visualforce email template that exists in the database into an instance of Messaging.SingleEmailMessage. Provides options for including attachment metadata only, attachment metadata and content, or excluding attachments.
public static Messaging.SingleEmailMessage renderStoredEmailTemplate(String templateId, String whoId, String whatId, Messaging.AttachmentRetrievalOption attachmentRetrievalOption)
-
templateId:
Type: String
An email template that exists in the database, such as text, HTML, custom, and Visualforce templates.
-
whoId:
Type: String
The identifier of an object in the database, typically a contact, lead, or user. The database record for that object is read and used in merge field processing.
-
whatId:
Type: String
Identifies
an object in the database, like an account or opportunity. The record for that object is read and used in merge field processing.
-
attachmentRetrievalOption:
Type: Messaging.AttachmentRetrievalOption
Specifies options for including attachments in the
fileAttachmentsproperty of the returnedMessaging.SingleEmailMessage. Set to one of the Messaging.AttachmentRetrievalOption values to include attachment metadata only, attachment metadata and content, or to exclude attachments.When the attachmentRetrievalOption parameter is not set to
NONE, theentityAttachmentsproperty ofMessaging.SingleEmailMessagecontains the ID of the Salesforce content objects to attach (ContentVersion or Document). ThefileAttachmentsproperty contains the IDs of attachments, in addition to all the IDs in theentityAttachmentsproperty. As a result, the ID values inentityAttachmentsare duplicates of the IDs in thefileAttachmentsproperty. If you callrenderStoredEmailTemplate()by passing theMETADATA_WITH_BODYoption, and send the rendered email message, the email will contain duplicate attachments. Before using the returned email message with sendEmail(emails, allOrNothing), you can remove attachments fromfileAttachmentsthat are duplicated inentityAttachments.
Type: Messaging.SingleEmailMessage
Executing the renderStoredEmailTemplate method counts toward the SOQL governor limit as one query.
Renders a text, custom, HTML, or Visualforce email template that exists in the database into an instance of Messaging.SingleEmailMessage. Provides options for including attachment metadata only, attachment metadata and content, or excluding attachments.
public static Messaging.SingleEmailMessage renderStoredEmailTemplate(String templateId, String whoId, String whatId, Messaging.AttachmentRetrievalOption attachmentRetrievalOption, Boolean updateEmailTemplateUsage)
-
templateId:
Type: String
An email template that exists in the database, such as text, HTML, custom, and Visualforce templates.
-
whoId:
Type: String
The identifier of an object in the database, typically a contact, lead, or user. The database record for that object is read and used in merge field processing.
-
whatId:
Type: String
Identifies
an object in the database, like an account or opportunity. The record for that object is read and used in merge field processing.
-
attachmentRetrievalOption:
Type: Messaging.AttachmentRetrievalOption
Specifies options for including attachments in the
fileAttachmentsproperty of the returnedMessaging.SingleEmailMessage. Set to one of the Messaging.AttachmentRetrievalOption values to include attachment metadata only, attachment metadata and content, or to exclude attachments.When the attachmentRetrievalOption parameter is not set to
NONE, theentityAttachmentsproperty ofMessaging.SingleEmailMessagecontains the ID of the Salesforce content objects to attach (ContentVersion or Document). ThefileAttachmentsproperty contains the IDs of attachments, in addition to all the IDs in theentityAttachmentsproperty. As a result, the ID values inentityAttachmentsare duplicates of the IDs in thefileAttachmentsproperty. If you callrenderStoredEmailTemplate()by passing theMETADATA_WITH_BODYoption, and send the rendered email message, the email will contain duplicate attachments. Before using the returned email message with sendEmail(emails, allOrNothing), you can remove attachments fromfileAttachmentsthat are duplicated inentityAttachments. -
updateEmailTemplateUsage:
Type: Boolean
Specifies whether the usage field in the EmailTemplate record is updated upon successful rendering.
Type: Messaging.SingleEmailMessage
Executing the renderStoredEmailTemplate method counts toward the SOQL governor limit as one query.