How to Set “From” Address in SingleEmailMessage?

How to Set “From” Address in SingleEmailMessage?

On December 9, 2025, Posted by , In Apex, With Comments Off on How to Set “From” Address in SingleEmailMessage?
How to Set  "From" Address in SingleEmailMessage?

When sending emails using Salesforce’s SingleEmailMessage, many developers encounter the challenge of setting a specific “From” address. By default, Salesforce uses the email address of the user triggering the email as the “From” address. However, there are methods to override this behavior and set “From” Address in SingleEmailMessage. Let’s explore the solutions.

Question:

How can I set a specific “From” address, such as doNotReply@blahblah.com, when sending an email using the Messaging.SingleEmailMessage class in Salesforce? My current implementation changes only the display name and not the actual email address.

Sample Code with Current Issue:

public static void sendSingleMail(Id objId, Id templateId, String fromAddress) {
    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    mail.setReplyTo(fromAddress);
    mail.setTemplateId(templateId);
    mail.setTargetObjectId(objId);
    mail.saveAsActivity = false;
    mail.setSenderDisplayName(fromAddress);

    Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}

Here, while the setReplyTo and setSenderDisplayName methods adjust the reply-to and display name, the actual “From” email address remains the sender’s email.

Our Salesforce online training covers Admin, Developer, and AI, combining theory with hands-on projects to tackle real-world challenges. Gain expertise through industry-focused assignments and master Salesforce solutions for career growth.

Solution: Use Org-Wide Email Addresses

To specify a custom “From” address and configure it in Salesforce, you need to set an Org-Wide Email Address in Salesforce. This allows you to set an authorized email as the “From” address when sending emails.

Steps:

  1. Go to Setup in Salesforce and navigate to:
    Administration Setup → Email Administration → Organization-Wide Addresses.
  2. Create a new Org-Wide Email Address by specifying the email (e.g., doNotReply@blahblah.com). Salesforce will send a confirmation email to this address, and you must verify it before it can be used.
  3. Once verified, retrieve the Org-Wide Email Address ID. You can use either hardcoding or querying to set this in your email message.

Implementation Using Query:

public static void sendSingleMail(Id objId, Id templateId, String fromAddress) {
    // Query the Org-Wide Email Address ID
    OrgWideEmailAddress[] owea = [SELECT Id FROM OrgWideEmailAddress WHERE Address = :fromAddress LIMIT 1];
    
    if (!owea.isEmpty()) {
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setOrgWideEmailAddressId(owea[0].Id);
        mail.setTemplateId(templateId);
        mail.setTargetObjectId(objId);
        mail.saveAsActivity = false;

        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    } else {
        System.debug('Org-Wide Email Address not found for ' + fromAddress);
    }
}

In this code, the Org-Wide Email Address is queried dynamically based on the fromAddress parameter. If the queried result is not empty, the email uses the corresponding Id by calling the setOrgWideEmailAddressId method. This ensures the specified “From” address is applied without hardcoding the Org-Wide Email Address ID. If no match is found, an appropriate debug message is logged.

See also: Salesforce Picklist Fields

Key Considerations

Org-Wide Email Addresses must be verified before use. Salesforce sends a confirmation email to the address during setup.
If you anticipate using multiple “From” addresses, ensure each is set up as an Org-Wide Email Address.
The setOrgWideEmailAddressId method only accepts valid Org-Wide Email Address IDs.

Using this approach, you can set the “From” address in SingleEmailMessage effectively, making emails appear professional and consistent with your organization’s branding.

Summing Up

To set a “From” address in SingleEmailMessage, configuring and using Org-Wide Email Addresses in Salesforce is essential. By creating and verifying your desired email in the Email Administration settings, you can dynamically assign it using the setOrgWideEmailAddressId method. This ensures your emails not only comply with Salesforce’s security standards but also maintain professional consistency and enhance trust with your recipients. Take control of your email branding and make your communications stand out with this simple yet powerful feature.

Elevate Your Career with Salesforce Training in Dallas

Take your career to the next level with our Salesforce training in Dallas, designed to provide a deep understanding of Admin, Developer, and AI concepts. Our expert-led program blends theory with hands-on experience, ensuring you gain practical skills to solve real-world business challenges.

Gain an edge with personalized mentorship, interview coaching, and certification preparation, helping you stand out in the job market. With industry-focused projects and in-depth learning materials, you’ll develop problem-solving abilities and technical expertise that top employers value.

Stay ahead in the Salesforce ecosystem and unlock new career opportunities. Join our FREE demo session today and take the first step toward a successful career in Salesforce.!

Free tutorial in our website:

Role
Profiles
Page Layouts
Permission Sets
Reports and Dashboards
Workflow rules in Salesforce

Comments are closed.