1

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 = '[email protected]'];
    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 });

2 Answers 2

2

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

0

I think I have found a handy way to circumvent this limitation and send mass email from an organisation wide email address. This doesn't use the MassEmailMessage class, it uses the ListEmail and ListEmailIndividualRecipient tables directly. It derives from the Ojbect Reference Manual for the ListEmail object where under the field "Status" it says "Read-only except when list email is in draft state. Changing the status to Scheduled causes the list email to be sent. Valid values: Draft, Scheduled, Sent, Limit Error, Cancelled".

This got me thinking so I tried the following code.

ListEmail LE = New ListEmail(); // Create a New List Email

LE.HtmlBody = HtmlValue; // HtmlValue is the HTML email body, which can be pulled from a template

LE.Status='Draft'; // Important. Create as a draft

LE.Subject=Subject; // Set the Subject

LE.TextBody=Body; // Body is the text value of the email body

LE.Name=Subject; // Probably can use what you want here

LE.FromAddress=EmailAddress; // The verified organisation wide email address

LE.FromName=DisplayName; // Display name of the org wide email address

Insert LE; // Insert as we need the ID below

List LEIRs = New List (); // New list of recipients

For ( ID I : ListContactIDs ) // ListContactIDs - previously created list of contact ID to receive the email

{

LEIRs.Add( New ListEmailIndividualRecipient(ListEmailId = LE.ID,RecipientId = I) ); // Add contact to list

}

Insert LEIRs; // Insert recipient list

LE.Status='Scheduled'; // Set List email status to 'Scheduled' as per documentation

Update LE; // Update. This causes the List Email to be sent to all recipients. "Status" changes to Sent

The email are all sent out just as if they were list emails / mass emails.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .