[SalesForce] Set “from Email address” in MassEmailMessage

I am trying to send a mass email to list of contacts from the apex.
SingleEmailMessage has the option to set OrgWideEmailAddress but I don't see an option in MassEmailMessage.

Below is the code to set from email address in SingleEmailMessage.

 OrgWideEmailAddress[] owea = [select Id from OrgWideEmailAddress where Address = 'doNotReply@blahblah.com'];
    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    if ( owea.size() > 0 ) {
        mail.setOrgWideEmailAddressId(owea.get(0).Id);
    }

How to set from email address in MassEmailMessage.

 List<contact> lstcon=[Select Id,Name,Email,RecordTypeId,RecordType.Name From Contact where RecordType.Name = 'OrgHead'];
 List<Id> lstids= new List<Id>();
 for(Contact c:lstcon)
 {
  lstids.add(c.id);
 }
 EmailTemplate et=[Select id,subject,body from EmailTemplate where name = 'CTemplate' limit 1];

 Messaging.MassEmailMessage mail = new Messaging.MassEmailMessage();
 mail.setTargetObjectIds(lstIds);
 mail.setTemplateId(et.id);
 Messaging.sendEmail(new Messaging.MassEmailMessage[] { mail });

Best Answer

We cannot set From Email when using Mass Email or MassEmailMessage.

Workaround from Knowledge Article:

User can change the email name on their My Email Settings before sending the mass email.

Steps:

Click Your Name | My Settings | Click on Email on the left | On My Email Settings you will see on right two boxes, you can change as below:

              How would you like your name to appear on your outgoing email? 
             *Email Name

              What email address would you like to use as your return address? 
             *Email Address 

Change and Save.

Note: The personal email associated with your salesforce account will not change.

Here is a link to the existing idea on Ideaexchange:

https://success.salesforce.com/ideaview?id=08730000000IMvLAAW

Related Topic