Messaging Class
Messaging Methods
The following are methods for Messaging. All are instance methods.
extractInboundEmail(source, includeForwardedAttachments)
Signature
public static Messaging.InboundEmail extractInboundEmail(Object source, Boolean includeForwardedAttachments)
Parameters
- source
- Type: Object
- An instance of Messaging.InboundEmail.BinaryAttachment whose MimeTypeSubtype is message/rfc822 or 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 true to provide all attachments, even attachments in embedded emails in the binaryAttachments and textAttachments properties of the returned value. Set to false to provide only the attachments that are at the top level of the source email.
Return Value
Type: Messaging.InboundEmail
reserveMassEmailCapacity(amountReserved)
Signature
public Void reserveMassEmailCapacity(Integer amountReserved)
Parameters
- amountReserved
- Type: Integer
Return Value
Type: Void
Usage
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.
reserveSingleEmailCapacity(amountReserved)
Signature
public Void reserveSingleEmailCapacity(Integer amountReserved)
Parameters
- amountReserved
- Type: Integer
Return Value
Type: Void
Usage
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.
sendEmail(emails, allOrNothing)
Signature
public Messaging.SendEmailResult[] sendEmail(Messaging.Email[] emails, Boolean allOrNothing)
Parameters
- emails
- Type: Messaging.Email[]
- allOrNothing
- Type: Boolean
-
The optional opt_allOrNone parameter specifies whether sendEmail prevents 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 is true.
Return Value
Type: Messaging.SendEmailResult[]
sendEmailMessage(emailMessageIds, allOrNothing)
Signature
public Messaging.SendEmailResult[] sendEmailMessage(List<ID> emailMessageIds, Boolean allOrNothing)
Return Value
Type: Messaging.SendEmailResult[]
If the emailMessageIds parameter is null, the method throws a System.IllegalArgumentException exception.
Usage
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.
Example
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.
1Case c = new Case();
2insert c;
3
4EmailMessage e = new EmailMessage();
5e.parentid = c.id;
6// Set to draft status.
7// This status is required
8// for sendEmailMessage().
9e.Status = '5';
10e.TextBody =
11 'Sample email message.';
12e.Subject = 'Apex sample';
13e.ToAddress = 'customer@email.com';
14insert e;
15
16List<Messaging.SendEmailResult>
17 results =
18 Messaging.sendEmailMessage(new ID[]
19 { e.id });
20
21System.assertEquals(1, results.size());
22System.assertEquals(true,
23 results[0].success);Versioned Behavior Changes
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.
renderEmailTemplate(whoId, whatId, bodies)
Signature
public static List<Messaging.RenderEmailTemplateBodyResult> renderEmailTemplate(String whoId, String whatId, List<String> bodies)
Parameters
- 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 whoId or whatId replaces the merge field reference.
Return Value
Type: List<Messaging.RenderEmailTemplateBodyResult>
Usage
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.
renderStoredEmailTemplate(templateId, whoId, whatId)
Signature
public static Messaging.SingleEmailMessage renderStoredEmailTemplate(String templateId, String whoId, String whatId)
Parameters
- 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.
Return Value
Usage
Executing the renderStoredEmailTemplate method counts toward the SOQL governor limit as one query.
renderStoredEmailTemplate(templateId, whoId, whatId, attachmentRetrievalOption)
Signature
public static Messaging.SingleEmailMessage renderStoredEmailTemplate(String templateId, String whoId, String whatId, Messaging.AttachmentRetrievalOption attachmentRetrievalOption)
Parameters
- 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 fileAttachments property of the returned Messaging.SingleEmailMessage. Set to one of the Messaging.AttachmentRetrievalOption values to include attachment metadata only, attachment metadata and content, or to exclude attachments.
Return Value
Usage
Executing the renderStoredEmailTemplate method counts toward the SOQL governor limit as one query.
renderStoredEmailTemplate(templateId, whoId, whatId, attachmentRetrievalOption, updateEmailTemplateUsage)
Signature
public static Messaging.SingleEmailMessage renderStoredEmailTemplate(String templateId, String whoId, String whatId, Messaging.AttachmentRetrievalOption attachmentRetrievalOption, Boolean updateEmailTemplateUsage)
Parameters
- 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 fileAttachments property of the returned Messaging.SingleEmailMessage. Set to one of the Messaging.AttachmentRetrievalOption values to include attachment metadata only, attachment metadata and content, or to exclude attachments.
- updateEmailTemplateUsage
- Type: Boolean
- Specifies whether the usage field in the EmailTemplate record is updated upon successful rendering.
Return Value
Usage
Executing the renderStoredEmailTemplate method counts toward the SOQL governor limit as one query.