+ Start a Discussion
Steven ShenSteven Shen 

How to change default email from address in apex code

We use SingleEmailMessage to send a email and want to set the from address as the record owner.
As known, the default email from address is the salesforce user who runs this code snippet. Most existing answers recommend us to use orgwideEmailAddress but we think orgwideEmailAddress is not suitable for our requirement bacause from email address loads dynamically rather than a static address.
We also know setReplyTo and setSenderDisplayName method. Actually, the email from user name is shown as expected, but the from address is not conform. It looks strange, doesn't it?
Does anyone know how to change the default email from address as a dynamic user, such as a record owner or the manager of a record owner?
sowmya Inturi 9sowmya Inturi 9
Hi Steven,
You cannot give dynamic From Address!
You can only set the Current user or orgwideEmailAddress in it.

Steven ShenSteven Shen
Hi sowmya,
Thank you for your answer.
Could you tell me why we cannot set a dynamic From Address?
If we need to set the from display name as the case owner name but the from email address is not the case owner email,  it's hard to understand the from info, right?
Maybe customers consider this email as a spam.

Dushyant SonwarDushyant Sonwar
HI Steven,

Salesforce only allow setting from emailaddress of current user and orgwideemailaddresses that has been verified. It has some limitation like setting from email address dynamically or sending daily email limit etc.
Reasons might be there are other small or big businesses revolving around those limitations :)

If you want to more control over such as changing from address dynamically and email tracking , then you need to integrate with any third party email sending api such as mailgun , mandrill , sendgrid , mailchimp etc.

Hope this helps.