+ Start a Discussion
Jeffrey KoJeffrey Ko 

Question on Apex SingleEmailMessage - how to prevent sender from getting copied (i.e. CC) in outbound messages

I am currently working on an Apex trigger, such that when the Account Name is modified, it will send an email to a specific list of email addresses defined in a custom setting.  I am using the SingleEmailMessage API to do this, and the email uses a a plain-text email template.

I am facing 2 issues right, which I am hoping someone may know the answer to:
  • Whenever Person A changes the Account Name, it sends the email to person_b@test.ca (mailto:person_b@test.ca) and person_c@test.com (mailto:person_c@test.com).  However, everytime this happens, Person A will get copied in the email (in the email's CC) for some reason!!   Is there a way to turn this off somehow? 
  • We'd like the email to be sent from a specific person (instead of the actual user who made the update).  However, there is no way to set the sender in the SingleEmailMessage.  Is there another way to do this?  We are currently looking into doing an Email Service to achieve this, but was wondering if there is an easier way


Any help or suggestion will be appreciated.

Thanks,
Jeffrey
Swayam@SalesforceGuySwayam@SalesforceGuy

Hi Jeffrey,

Use setTargetObjectId(targetObjectId) method, and change your code accordingly, If possible you can share the code here, so that i will exactly let know where to make the changes

--
Thanks,
Swayam 
@salesforceguy

Jeffrey KoJeffrey Ko
Below is a code sample ... basically, all 3 emails, the running user who executed this code in the Developer Console will get copied in the email.  If I try the same code in my Developer Edition environment, it works fine (i.e. I will not get copied).

NOTE: the email used below were changed to a test email.
 
List<Messaging.SingleEmailMessage> singleEmailList=new List<Messaging.SingleEmailMessage>();

String[] toAddresses =  new String[] {'peter@test.com'};

// Email Test 1
Messaging.SingleEmailMessage singleEmail1 = new Messaging.SingleEmailMessage();
singleEmail1.setSenderDisplayName('Salesforce CRM Support - TEST');
singleEmail1.setReplyTo('jeffrey@test.com');
singleEmail1.setSubject('Test Email Only - setBccSender=false');
singleEmail1.setPlainTextBody('Test Email Only ... ');
singleEmail1.setBccSender(false);
singleEmail1.setToAddresses(toAddresses);
singleEmailList.add(singleEmail1);

// Email Test 2
Messaging.SingleEmailMessage singleEmail2 = new Messaging.SingleEmailMessage();
singleEmail2.setSenderDisplayName('Salesforce CRM Support - TEST');
singleEmail2.setReplyTo('jeffrey@test.com');
singleEmail2.setSubject('Test Email Only - setBccSender=true');
singleEmail2.setPlainTextBody('Test Email Only ... ');
singleEmail2.setBccSender(true);  // TRUE
singleEmail2.setToAddresses(toAddresses);
singleEmailList.add(singleEmail2);

// Email Test 3
Messaging.SingleEmailMessage singleEmail3=new Messaging.SingleEmailMessage();
singleEmail3.setTemplateId([EMAIL_TEMPLATE_ID]);
singleEmail3.setSaveAsActivity(false);
singleEmail3.setWhatId([ACCOUNT_ID]);
singleEmail3.setTargetObjectId([CONTACT_ID]);
singleEmailList.add(singleEmail3);


Messaging.SendEmail(singleEmailList);

I have created a case with Salesforce Support as well to see if they know of the reason.