Newer Version Available
Outbound Email
You can use Salesforce to track the status of email in HTML format, including the date the email was sent, first opened and last opened, and the total number of times it was opened.
- SingleEmailMessage
- Instantiates an email object used for sending a single email message. The syntax
is:
1Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); - MassEmailMessage
- Instantiates an email object used for sending a mass email message. The syntax
is:
1Messaging.MassEmailMessage mail = new Messaging.MassEmailMessage(); - Messaging
- Includes the static sendEmail method, which sends
the email objects you instantiate with either the SingleEmailMessage or MassEmailMessage
classes, and returns a SendEmailResult object.
The syntax for sending an email is:
1Messaging.sendEmail(new Messaging.Email[] { mail } , opt_allOrNone);where Email is either Messaging.SingleEmailMessage or Messaging.MassEmailMessage.
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.
- Includes the static reserveMassEmailCapacity and
reserveSingleEmailCapacity methods, which can be
called before sending any emails to ensure that the sending organization won't exceed its
daily email limit when the transaction is committed and emails are sent. The syntax
is:
1Messaging.reserveMassEmailCapacity(count);and
1Messaging.reserveSingleEmailCapacity(count);where count indicates the total number of addresses that emails will be sent to.
- The email is not sent until the Apex transaction is committed.
- The email address of the user calling the sendEmail method is inserted in the From Address field of the email header. All email that is returned, bounced, or received out-of-office replies goes to the user calling the method.
- Maximum of 10 sendEmail methods per transaction. Use the Limits methods to verify the number of sendEmail methods in a transaction.
- Single email messages sent with the sendEmail method count against the sending organization's daily single email limit. When this limit is reached, calls to the sendEmail method using SingleEmailMessage are rejected, and the user receives a SINGLE_EMAIL_LIMIT_EXCEEDED error code. However, single emails sent through the application are allowed.
- Mass email messages sent with the sendEmail method count against the sending organization's daily mass email limit. When this limit is reached, calls to the sendEmail method using MassEmailMessage are rejected, and the user receives a MASS_MAIL_LIMIT_EXCEEDED error code.
- Any error returned in the SendEmailResult object indicates that no email was sent.
Messaging.SingleEmailMessage has a method called setOrgWideEmailAddressId. It accepts an object ID to an OrgWideEmailAddress object. If setOrgWideEmailAddressId is passed a valid ID, the OrgWideEmailAddress.DisplayName field is used in the email header, instead of the logged-in user's Display Name. The sending email address in the header is also set to the field defined in OrgWideEmailAddress.Address.
For more information, see Organization-Wide Email Addresses in the Salesforce online help.
Example
1// First, reserve email capacity for the current Apex transaction to ensure
2// that we won't exceed our daily email limits when sending email after
3// the current transaction is committed.
4Messaging.reserveSingleEmailCapacity(2);
5
6// Processes and actions involved in the Apex transaction occur next,
7// which conclude with sending a single email.
8
9// Now create a new single email message object
10// that will send out a single email to the addresses in the To, CC & BCC list.
11Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
12
13// Strings to hold the email addresses to which you are sending the email.
14String[] toAddresses = new String[] {'user@acme.com'};
15String[] ccAddresses = new String[] {'smith@gmail.com'};
16
17
18// Assign the addresses for the To and CC lists to the mail object.
19mail.setToAddresses(toAddresses);
20mail.setCcAddresses(ccAddresses);
21
22// Specify the address used when the recipients reply to the email.
23mail.setReplyTo('support@acme.com');
24
25// Specify the name used as the display name.
26mail.setSenderDisplayName('Salesforce Support');
27
28// Specify the subject line for your email address.
29mail.setSubject('New Case Created : ' + case.Id);
30
31// Set to True if you want to BCC yourself on the email.
32mail.setBccSender(false);
33
34// Optionally append the salesforce.com email signature to the email.
35// The email address of the user executing the Apex Code will be used.
36mail.setUseSignature(false);
37
38// Specify the text content of the email.
39mail.setPlainTextBody('Your Case: ' + case.Id +' has been created.');
40
41mail.setHtmlBody('Your case:<b> ' + case.Id +' </b>has been created.<p>'+
42 'To view your case <a href=https://yourInstance.salesforce.com/'+case.Id+'>click here.</a>');
43
44// Send the email you have created.
45Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });