sendEmailMessage()
Syntax
For Enterprise SOAP:
1SendEmailResult[] = connection.sendEmailMessage( String[] draftEmailIds);For Partner SOAP:
1SendEmailResult[] = connection.sendEmailMessage( ID[] draftEmailIds);Usage
Use this call with Lightning Platform AppExchange applications, custom applications, or other applications outside of Salesforce to send up to 10 draft email messages. The messages 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 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.
Sample CodeāJava
This sample creates a case and a draft email message, and sets the message fields, including the From, To, CC, and BCC recipients, subject, and body text. It also 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.
1public void doSendEmail() {
2 try {
3 //Create a case
4 Case theCase = new Case();
5 theCase.setSubject("Sample Case");
6 SaveResult[] saveResult = connection.create(new SObject[] { theCase });
7 String caseId = saveResult[0].getId();
8
9 //Create a draft EmailMessage
10 EmailMessage message = new EmailMessage();
11 message.setParentId(theCase.getId());
12 message.setBccAddress("bcc@email.com");
13 message.setCcAddress("cc1@salesforce.com; cc2@email.com");
14 message.setSubject("This is how you use the sendEmailMessage method.");
15 message.setFromAddress("from@email.com");
16 message.setFromName("Sample Code");
17 message.setTextBody("This is the text body of the message.");
18 message.setStatus("5"); //"5" means Draft
19 message.setToAddress("to@email.com");
20 saveResult = connection.create(new SObject[] { message });
21 String emailMessageId = saveResult[0].getId();
22
23 //Create an attachment for the draft EmailMessage
24 Attachment att = new Attachment();
25 byte[] fileBody = new byte[1000000];
26 att.setBody(fileBody);
27 att.setName("attachment");
28 att.setParentId(emailMessageId);
29 connection.create(new SObject[] { att });
30
31 //Send the draft EmailMessage
32 SendEmailResult[] results = connection.sendEmailMessage(messages);
33 if (results[0].isSuccess()) {
34 System.out.println("The email was sent successfully.");
35 } else {
36 System.out.println("The email failed to send: " +
37 results[0].getErrors()[0].getMessage());
38 }
39 } catch (ConnectionException ce) {
40 ce.printStackTrace();
41 }Arguments
None.