sendEmail()
Syntax
For single email messages:
SendEmailResult = connection.sendEmail(SingleEmailMessage emails[]);
For mass email messages:
SendEmailResult = connection.sendEmail(MassEmailMessage emails[]);
Usage
Use this call with Lightning Platform AppExchange applications, custom applications, or other applications outside of Salesforce to send individual and mass email. The email can include all standard email attributes (such as subject line and blind carbon copy address), use Salesforce email templates, and be in plain text or HTML format. You can use Salesforce to track the status of HTML email, including the date the email was sent, first opened, last opened, and the total number of times it was opened. (See “Tracking HTML Email” in Salesforce Help for more information.)
The email address of the logged-in user is inserted in the From Address field of the email header. All return email and out-of-office replies go to the logged-in user. If bounce management is enabled and SingleEmailMessage.targetObjectId or MassEmailMessage.targetObjectIds is set, bounces are processed by Salesforce automatically, and the appropriate records are updated; otherwise, they go to the logged-in user. Bounce management works for contacts and leads only.
SingleEmailMessage has an optional field called OrgWideEmailAddressId, an object ID to an OrgWideEmailAddress object. If OrgWideEmailAddressId is set, 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.
Sample Code—Java
This sample creates an email message and sets its fields, including the To, CC and BCC recipients, subject, and body text. It also sets a recipient to the ID of the logged-in user using the setTargetObjectId method, which causes the email to be sent to the email address of the specified user. The sample creates an attachment and sends the email message with the attachment. Finally, it writes a status message or an error message, if any, to the console.
public void doSendEmail() {
try {
EmailFileAttachment efa = new EmailFileAttachment();
byte[] fileBody = new byte[1000000];
efa.setBody(fileBody);
efa.setFileName("attachment");
SingleEmailMessage message = new SingleEmailMessage();
message.setBccAddresses(new String[] {
"someone@salesforce.com"
});
message.setCcAddresses(new String[] {
"person1@salesforce.com", "person2@salesforce.com", "003xx00000a1b2cAAC"
});
message.setBccSender(true);
message.setEmailPriority(EmailPriority.High);
message.setReplyTo("person1@salesforce.com");
message.setSaveAsActivity(false);
message.setSubject("This is how you use the " + "sendEmail method.");
// We can also just use an id for an implicit to address
GetUserInfoResult guir = connection.getUserInfo();
message.setTargetObjectId(guir.getUserId());
message.setUseSignature(true);
message.setPlainTextBody("This is the humongous body "
+ "of the message.");
EmailFileAttachment[] efas = { efa };
message.setFileAttachments(efas);
message.setToAddresses(new String[] { "person3@salesforce.com" });
SingleEmailMessage[] messages = { message };
SendEmailResult[] results = connection.sendEmail(messages);
if (results[0].isSuccess()) {
System.out.println("The email was sent successfully.");
} else {
System.out.println("The email failed to send: "
+ results[0].getErrors()[0].getMessage());
}
} catch (ConnectionException ce) {
ce.printStackTrace();
}
}
This example shows how to send an email with the opt-out setting enforced. Recipients are specified by their IDs. The SendEmailOptOutPolicy.FILTER option causes the email to be sent only to recipients that haven’t opted out from email.
SingleEmailMessage message = new SingleEmailMessage();
// Set recipients to two contact IDs.
// Replace IDs with valid record IDs in your org.
message.setToAddresses(new String[] { "003D000000QDexS", "003D000000QDfW5" });
message.setOptOutPolicy(SendEmailOptOutPolicy.FILTER);
message.setSubject("Opt Out Test Message");
message.setPlainTextBody("This is the message body.");
SingleEmailMessage[] messages = { message };
SendEmailResult[] results = connection.sendEmail(messages);
if (results[0].isSuccess()) {
System.out.println("The email was sent successfully.");
} else {
System.out.println("The email failed to send: "
+ results[0].getErrors()[0].getMessage());
}
Sample Code—C#
This sample creates an email message and sets its fields, including the To, CC and BCC recipients, subject, and body text. It also sets a recipient to the ID of the logged-in user using the setTargetObjectId method, which causes the email to be sent to the email address of the specified user. The sample creates an attachment and sends the email message with the attachment. Finally, it writes a status message or an error message, if any, to the console.
public void doSendEmail()
{
try
{
EmailFileAttachment efa = new EmailFileAttachment();
byte[] fileBody = new byte[1000000];
efa.body = fileBody;
efa.fileName = "attachment";
SingleEmailMessage message = new SingleEmailMessage();
message.setBccAddresses(new String[] {
"someone@salesforce.com"
});
message.setCcAddresses(new String[] {
"person1@salesforce.com", "person2@salesforce.com", "003xx00000a1b2cAAC"
});
message.bccSender = true;
message.emailPriority = EmailPriority.High;
message.replyTo = "person1@salesforce.com";
message.saveAsActivity = false;
message.subject = "This is how you use the " + "sendEmail method.";
// We can also just use an id for an implicit to address
GetUserInfoResult guir = binding.getUserInfo();
message.targetObjectId = guir.userId;
message.useSignature = true;
message.plainTextBody = "This is the humongous body "
+ "of the message.";
EmailFileAttachment[] efas = { efa };
message.fileAttachments = efas;
message.toAddresses = new String[] { "person3@salesforce.com" };
SingleEmailMessage[] messages = { message };
SendEmailResult[] results = binding.sendEmail(messages);
if (results[0].success)
{
Console.WriteLine("The email was sent successfully.");
}
else
{
Console.WriteLine("The email failed to send: "
+ results[0].errors[0].message);
}
}
catch (SoapException e)
{
Console.WriteLine("An unexpected error has occurred: " +
e.Message + "\n" + e.StackTrace);
}
}
This example shows how to send an email with the opt-out setting enforced. Recipients are specified by their IDs. The SendEmailOptOutPolicy.FILTER option causes the email to be sent only to recipients that haven’t opted out from email.
SingleEmailMessage message = new SingleEmailMessage();
// Set recipients to two contact IDs.
// Replace IDs with valid record IDs in your org.
message.toAddresses = new String[] { "003D000000QDexS", "003D000000QDfW5" };
message.optOutPolicy = SendEmailOptOutPolicy.FILTER;
message.subject = "Opt Out Test Message";
message.plainTextBody = "This is the message body.";
SingleEmailMessage[] messages = { message };
SendEmailResult[] results = binding.sendEmail(messages);
if (results[0].success)
{
Console.WriteLine("The email was sent successfully.");
} else {
Console.WriteLine("The email failed to send: "
+ results[0].errors[0].message);
}
BaseEmail
This table contains the arguments used in both single and mass email.
SingleEmailMessage
The following table contains the arguments single email uses in addition to the base email arguments.
Name | Type | Description |
---|---|---|
bccAddresses | string[] | Optional. An array of blind carbon copy (BCC) addresses or object IDs of
the contacts, leads, and users you’re sending the email to. This argument is
allowed only when a template isn’t used. The maximum size for this field is
4,000 bytes. The maximum total of toAddresses,
ccAddresses, and bccAddresses
per email is 150. All recipients in these three fields count against the
limit for email sent using Apex or the API. You can specify opt-out email options with the optOutPolicy field only for those recipients who were added by their IDs. Email addresses are verified to ensure that they have the correct format and haven’t been marked as bounced. If the BCC COMPLIANCE option is set at the organization level, the user can’t add BCC addresses on standard messages. The following error code is returned: BCC_NOT_ALLOWED_IF_BCC_COMPLIANCE_ENABLED. All emails must have a recipient value in at least one of the following fields:
|
ccAddresses | string[] | Optional. An array of carbon copy (CC) addresses or object IDs of the
contacts, leads, and users you’re sending the email to. This argument is
allowed only when a template isn’t used. The maximum size for this field is
4,000 bytes. The maximum total of toAddresses,
ccAddresses, and bccAddresses
per email is 150. All recipients in these three fields count against the
limit for email sent using Apex or the API. You can specify opt-out email options with the optOutPolicy field only for those recipients who were added by their IDs. Email addresses are verified to ensure that they have the correct format and haven’t been marked as bounced. All emails must have a recipient value in at least one of these fields.
|
charset | string | Optional. The character set for the email. If this value is null, the user's default value is used. Unavailable if specifying templateId because the template specifies the character set. |
documentAttachments | ID[] | Deprecated. Use entityAttachments instead. Optional. An array listing the ID of each Document you want to attach to the email. |
entityAttachments | ID[] | Optional. Array of IDs of Document, ContentVersion, or Attachment items to attach to the email. |
fileAttachments | EmailFileAttachment[] | Optional. An array listing the file names of the binary and text files you want to attach to the email. You can attach multiple files as long as the total size of all attachments doesn’t exceed 20 MB. |
htmlBody | string | Optional. The HTML version of the email, specified by the sender. The value is encoded according to the specification associated with the organization. |
inReplyTo | string | Optional. The In-Reply-To field of the outgoing email. Identifies the emails to which this one is a reply (parent emails). Contains the parent emails' Message-IDs. See RFC2822 - Internet Message Format. |
optOutPolicy | SendEmailOptOutPolicy (enumeration of type string) |
Optional. If you add contact, lead, or person
account recipients by ID instead of email address, this field determines
the behavior of the sendEmail()
call. By default, the opt-out settings for recipients added by their
email addresses aren’t checked and those recipients always receive the
email. Possible values of the SendEmailOptOutPolicy enumeration are:
The Send Non-Commercial Email permission isn’t respected. This field is available in API version 35.0 and later. |
orgWideEmailAddressId | ID | Optional. The object ID of the OrgWideEmailAddress associated with the outgoing email. OrgWideEmailAddress.DisplayName can’t be set if the senderDisplayName field is already set. |
plainTextBody | string | Optional. The text version of the email, specified by the sender. |
references | string | Optional. The References field of the outgoing email. Identifies an email thread. Contains the parent emails' Message-ID and References fields and possibly In-Reply-To fields. See RFC2822 - Internet Message Format. |
targetObjectId | ID | Optional. The object ID of the contact, lead, or user the email is sent
to. The object ID you enter sets the context and ensures that merge fields
in the template contain the correct data All emails must have a recipient value in at least one of these fields.
|
toAddresses | string[] | Optional. An array of email addresses or object IDs of the contacts,
leads, or users you’re sending the email to. This argument is allowed only
when a template isn’t used. The maximum size for this field is 4,000 bytes.
The maximum total of toAddresses,
ccAddresses, and bccAddresses
per email is 150. All recipients in these three fields count against the
limit for email sent using Apex or the API. You can specify opt-out email options with the optOutPolicy field only for those recipients who were added by their IDs. Email addresses are verified to ensure that they have the correct format and haven’t been marked as bounced. All emails must have a recipient value in at least one of these fields.
|
treatBodiesAsTemplate | boolean |
Optional. If set to true, the subject, plain text, and HTML
text bodies of the email are treated as template data. The merge fields
are resolved using the renderEmailTemplate() call. Default is false. This field is available in API version 35.0 and later. |
treatTargetObjectAsRecipient | boolean |
Optional. If set to true, the
targetObjectId (a contact, lead, or user) is the
recipient of the email. If set to false, the targetObjectId is supplied
as the WhoId field for template rendering but isn’t
a recipient of the email. The default is true. This field is available in API version 35.0 and later. In prior versions, the targetObjectId is always a recipient of the email. |
whatId | ID | Optional. If you specify a contact for the
targetObjectId field, you can specify a
whatId as well. This field helps to further ensure
that merge fields in the template contain the correct data. The value must
be one of the following types:
|
MassEmailMessage
The following table contains the arguments mass email uses in addition to the base email arguments.
Name | Type | Description |
---|---|---|
description | string | A value used internally to identify the object in the mass email queue. |
targetObjectIds | ID[] | An array of object IDs of the contacts, leads, or users the email is sent to. The object IDs you enter set the context and ensure that merge fields in the template contain the correct data. The objects must be of the same type (either all contacts, all leads, or all users). You can list up to 250 IDs per email. If you specify a value for the targetObjectIds field, optionally specify a whatId as well to set the email context to a user, contact, or lead. Specifying these IDs ensures that merge fields in the template contain the correct data. |
whatIds | ID[] | Optional. If you specify an array of contacts for the
targetObjectIds field, you can specify an array of
whatIds as well. Specifying these IDs ensures that
merge fields in the template contain the correct data. The values must be
one of these types.
|
EmailFileAttachment
The following table contains properties that the EmailFileAttachment uses in the SingleEmailMessage object to specify attachments passed in as part of the request, as opposed to a Document passed in using the documentAttachments argument.
Response
Fault
The following API status codes can be returned. Also, sendEmail() can return other errors when rendering email templates. See renderEmailTemplate() Faults.
BCC_NOT_ALLOWED_IF_BCC_COMPLIANCE_ENABLED
BCC_SELF_NOT_ALLOWED_IF_BCC_COMPLIANCE_ENABLED
EMAIL_NOT_PROCESSED_DUE_TO_PRIOR_ERROR