function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
monkeykingmonkeyking 

Different 'From' address for Single Email message

Hello All,

We have a business scenario where the 'From' address used in Single email message should be different based on the Counry(a custom field in our og).
I know how to set the 'From' address in Single Email message.
Just wondering are there any other methods to have different from address based on the countries.

Thanks is advance.

Regards,
Rajkumar CV
Ajay K DubediAjay K Dubedi
Hi,

To do this you must first set up a dedicated email address by navigating to Setup -> Administration Setup -> Email Administration -> Organization-Wide Addresses menu. Once you have created an org-wide address (note that Salesforce will require you to confirm the address prior to using it, so if you're going to be sending things from a junk address it would be wise to set up a catch-all mailbox so you receive the confirmation email), grab the Id from the URL and use the setOrgWideEmailAddressId(Id) method on your instance of Messaging.SingleEmailMessage.

If you want to avoid hard-coding an Id, after creating your Org-Wide Address you can query them:
 
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);
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com
monkeykingmonkeyking
Thank you for your reply