Immediately sends an email message.
For single email messages:
SendEmailResult = connection.sendEmail( BaseEmailSingleEmailMessage emails[]);
For mass email messages:
SendEmailResult = connection.sendEmail( BaseEmailMassEmailMessage emails[]);
Use this call with Force.comAppExchange 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 the Salesforce online 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 logged-in user. If bounce management is enabled, bounces are processed by Salesforce automatically, and the appropriate record is updated; otherwise, they go to the logged-in user.
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[] { "jdoe@example.com" }); message.setCcAddresses(new String[] { "jqpublic@ecample.com", "jdoe@salesforce.com" }); message.setBccSender(true); message.setEmailPriority(EmailPriority.High); message.setReplyTo("dcarroll@salesforce.com"); message.setSaveAsActivity(true); 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[] { "jdoe@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(); } }
/// Demonstrates how to send an email public void SendEmailSample() { // Create the byte array that needs to be attached to the email. // For demonstration, we're just attaching a blank array of roughly 1KB. // For a real attachment, the file would need to be loaded into the byte array. byte[] fileBody = new byte[1000]; EmailFileAttachment[] fileAttachments = new EmailFileAttachment[1]; EmailFileAttachment fileAttachment = new EmailFileAttachment(); fileAttachment.body = fileBody; fileAttachment.fileName = "Marketing Flyer"; fileAttachments[0] = fileAttachment; // Create the Email Message SingleEmailMessage[] messages = new SingleEmailMessage[1]; messages[0] = new SingleEmailMessage(); messages[0].bccAddresses = new string[] { "marketing@mycompany.com", "sales@mycompany.com" }; messages[0].ccAddresses = new string[] { "yourboss@yourcompany.com" }; messages[0].bccSender = true; messages[0].emailPriority = EmailPriority.Normal; messages[0].replyTo = "john.doe@mycompany.com"; messages[0].saveAsActivity = true; messages[0].subject = "Sample email sent via the API"; messages[0].useSignature = true; messages[0].plainTextBody = "Dear Customer,\n\nPlease buy our products.\n" + "Thanks,\n\nJohn Doe\nMarketing Vice President"; messages[0].fileAttachments = fileAttachments; // We can either set the To Address or just use an ID of a Contact, // Lead or User for an implicit To Address // messages[0].targetObjectId("00ID00000FooVsr"); messages[0].toAddresses = new string[] { "jane.doe@yourcompany.com" }; // Now send the email try { SendEmailResult[] result = binding.sendEmail(messages); if (result[0].success) { Console.WriteLine("The email was sent successfully"); } else { Console.WriteLine("The email failed to send: " + result[0].errors[0].message); } } catch (SoapException e) { Console.WriteLine(e.Message); Console.WriteLine(e.StackTrace); Console.WriteLine(e.InnerException); } }
| Name | Type | Description |
|---|---|---|
| bccSender | boolean | Indicates whether the email sender receives a copy of the email that is sent. For a mass mail, the sender is only copied on the first email sent. |
| saveAsActivity | boolean | Optional. The default value is true, meaning the email is saved as an activity. This argument only applies if the recipient list is based on targetObjectId or targetObjectIds. If HTML email tracking is enabled for the organization, you will be able to track open rates. |
| useSignature | boolean | Indicates whether the email includes an email signature if the user has one configured. The default is true, meaning if the user has a signature it is included in the email unless you specify false. |
| emailPriority | picklist | Optional. The priority of the email.
|
| replyTo | string | Optional. The email address that receives the message when a recipient replies. This cannot be set if you are using a Visualforce email template that specifies a replyTo value. |
| subject | string | Optional. The email subject line. If you are using an email template and attempt to override the subject line, an error message is returned. |
| templateId | ID | The ID of the template to be merged to create this email. |
| senderDisplayName | string | Optional. The name that appears on the From line of the email. This cannot be set if the object associated with a OrgWideEmailAddressId for a SingleEmailMessage has defined its DisplayName field. |
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. The
maximum allowed is 25. This argument is allowed only when a template
is not used. If the BCC COMPLIANCE option is set at the organization level, the user cannot 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:
|
| ccAddresses | string[] | Optional. An array of carbon copy (CC) addresses. The maximum allowed is 25. This argument is allowed only when a template is not used. |
| 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[] | Optional. An array listing the ID of each Document you want to attach to the email. You can attach multiple documents as long as the total size of all attachments does not exceed 10 MB. |
| 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 does not exceed 10 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. |
| orgWideEmailAddressId | ID | Optional. The object ID of the OrgWideEmailAddress associated with the outgoing email. OrgWideEmailAddress.DisplayName cannot 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
will be sent to. The object ID you enter sets the context and ensures
that merge fields in the template contain the correct data Do not enter the object IDs of records that have the Email Opt Out option selected. All emails must
have a recipient value in at least one of the following:
|
| toAddresses | string[] | Optional. An array of email address you are sending the email to. The maximum allowed is 100. This argument is allowed only when a template is not used. |
| whatId | ID | Optional. If you specify a contact for the targetObjectId field, you can specify a whatId as well. This
helps to further ensure that merge fields in the template contain
the correct data. The value must be one of the following types:
|
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 will be 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. This ensures that merge fields in the
template contain the correct data. Do not enter the object IDs of records that have the Email Opt Out option selected. All emails must
have a recipient value in at least one of the following:
|
| whatIds | ID[] | Optional. If you specify an array of contacts for the targetObjectIds field, you can specify an array of whatIds as well. This helps to further ensure that merge
fields in the template contain the correct data. The values must be
one of the following types:
|
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.
| Property | Type | Description |
|---|---|---|
| body | base64 | The attachment itself. |
| contentType | string | Optional. The attachment's Content-Type. |
| fileName | string | The name of the file to attach. |
| inline | boolean | Optional. Specifies a Content-Disposition of inline (true) or attachment (false). In most cases, inline content is displayed to the user when the message is opened. Attachment content requires user action to be displayed. |