Print this page

Setting a 'from' address in SingleEmailMessage

Knowledge Article Number 000232863

I want to set a from address in a salesforce SingleEmailMessage, but cant see a way to do this, and there is no standard method in the documentation.

I want an email to be sent from, however, the email of the internal user editing the record (which triggers the email) is being sent as a from address. I want an address which is not specific to a User.


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 = ''];
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
if ( owea.size() > 0 ) {

promote demote