Loading

Override 'From' address with SingleEmailMessage

Udgivelsesdato: Jun 3, 2026
Beskrivelse

In Salesforce Apex, the Messaging.SingleEmailMessage class is used to send outbound emails programmatically. By default, outbound emails are sent from the email address of the running user. In some scenarios, it is necessary to override the From address — for example, to send emails from a shared support address or a company-wide noreply address.
This article explains how to override the From address on a Messaging.SingleEmailMessage instance by using an Org-Wide Email Address configured in Salesforce Setup.

Løsning

How to Override the From Address

To override the From address on a Salesforce Apex SingleEmailMessage, use the setOrgWideEmailAddressId(ID) method. This method accepts the Salesforce ID of an Org-Wide Email Address record and uses that address as the From address when the email is sent.

Steps

  1. In Salesforce Setup, navigate to Email > Organization-Wide Addresses and create the email address you want to use as the From address, or identify an existing one.
  2. In Apex, query the OrgWideEmailAddress object to retrieve the ID of the desired email address. For example, query OrgWideEmailAddress where the Address field equals the address you want to use (such as 'support@yourcompany.com') and retrieve the Id of the first matching record.
  3. Create a new instance of Messaging.SingleEmailMessage and call the setOrgWideEmailAddressId() method, passing in the ID retrieved in step 2.
  4. Set the remaining email properties (recipient, subject, body) and call Messaging.sendEmail() to send the email.
  5. If the OrgWideEmailAddress query returns no results, ensure the address has been created and verified in Setup under Email > Organization-Wide Addresses.

 

Yderligere ressourcer

Reference: SingleEmailMessage methods 

Vidensartikelnummer

000386605

 
Indlæser
Salesforce Help | Article