Messaging Class

Contains messaging methods used when sending a single or mass email.

Namespace

System

Messaging Methods

The following are methods for Messaging. All are instance methods.

reserveMassEmailCapacity(amountReserved)

Reserves email capacity to send mass email to the specified number of email addresses, after the current transaction commits.

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)

Reserves email capacity to send single email to the specified number of email addresses, after the current transaction commits.

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)

Sends the list of email objects instantiated with either SingleEmailMessage or MassEmailMessage and returns a list of SendEmailResult objects.

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)

Sends up to 10 draft email messages as defined by the specified email message IDs and returns a list of SendEmailResult objects.

Signature

public Messaging.SendEmailResult[] sendEmailMessage(List<ID> emailMessageIds, Boolean allOrNothing)

Parameters

emailMessageIds
Type: List<ID>
allOrNothing
Type: Boolean

Return Value

Type: Messaging.SendEmailResult[]

Usage

The sendEmailMessage method assumes that the optional opt_allOrNone parameter is always false and ignores the value you set. This optional parameter specifies whether sendEmailMessage 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).

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.

Case c = new Case();
insert c;

EmailMessage e = new EmailMessage();
e.parentid = c.id;
// Set to draft status.
// This status is required 
// for sendEmailMessage().
e.Status = '5'; 
e.TextBody = 
  'Sample email message.';
e.Subject = 'Apex sample';
e.ToAddress = 'customer@email.com';
insert e;

List<Messaging.SendEmailResult> 
  results = 
  Messaging.sendEmailMessage(new ID[] 
    { e.id });

System.assertEquals(1, results.size());
System.assertEquals(true, 
                    results[0].success);

renderEmailTemplate(whoId, whatId, bodies)

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.

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

This method can be used in situations where you want to dynamically compose blocks of text that are enriched with data from the database. The rendered blocks of text can then be used, for example, to compose and send an email or update a text value in another database record.

renderStoredEmailTemplate(templateId, whoId, whatId)

Renders a text, custom, HTML, or Visualforce email template that exists in the database into an instance of Messaging.SingleEmailMessage.

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

Type: Messaging.SingleEmailMessage