Hello Everyone, In this tutorial, I am going to provide a step-by-step procedure to setup Organization-Wide Address in salesforce and also provide an Apex code example that explains, how you will use your Organization-Wide Address in Salesforce email class.

After completing this tutorial, you’ll able to:
  • Setup Organization-Wide Address in Salesforce.
  • Use Organization-Wide Address in Apex.
So let's begin.

Setup Organization-Wide Address in Salesforce

First set up Organization-Wide Address (a dedicated email address) by navigating to Setup | Administration Setup | Email Administration | Organization-Wide Addresses menu.

Step 1: Click on Add button.

Step 2: Fill Display Name and Email Address.

In the below image, you can see there are two option,


1. Allow All Profiles to Use this From Address - By selecting this providing email address is accessible through all profile.

2. Allow Only Selected Profiles to Use the From Address - By selecting this, you can choose which profile can access your org-wide email address. You can choose multiple profiles by pressing the CTRL button at a time.


Step 3: Click on the Save button. After clicking the save button a verification email will be sent to your provided email address. Go to that email and verify.


After clicking on the link that mentions in verification email, the following window will appear.



Step 4: Click on the continue button and again navigate to Setup | Administration Setup | Email Administration | Organization-Wide Addresses menu. Now you can see your org-wide email address with verified status.




Use Organization-Wide Address in Apex

In apex, We use the organization-wide address to replace the sender name and Id (from email address name and email-Id). By default salesforce, email messaging class used logged in user email address and name to send email, so in many scenarios, we have replaced this default functionality by using Salesforce organization-wide address.

Note: Only verified organization-wide email addresses can be used in Apex email messaging class.

Example:
 public class MyEmailHandler { 
    
    public void SendEmail(){  
        List<Messaging.SingleEmailMessage> lstMail=new List<Messaging.SingleEmailMessage>();
        // Query from Organization-Wide Email Address       
        List<OrgWideEmailAddress> lstEmailAddress=[select Id from OrgWideEmailAddress WHERE Address='salesforcescool@gmail.com'];
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();   
        String[] toAddresses = new String[] {'smanisheng@gmail.com'}; 
        mail.setToAddresses(toAddresses); 
        // Set Organization-Wide Email Address Id
        mail.setOrgWideEmailAddressId(lstEmailAddress[0].Id); 
        mail.setSubject('SalesforceScool'); 
        mail.setBccSender(false); 
        mail.setUseSignature(false); 
        mail.setHtmlBody('This email is for testing purpose of Organization-Wide Email Address');
        lstMail.add(mail);        
        if(lstMail.size()>0){
            Messaging.SendEmailResult[] results = Messaging.sendEmail(lstMail);
            if (results[0].success) {
                System.debug('The email was sent successfully.');                
            } 
            else {
                System.debug('The email failed to send: ' + results[0].errors[0].message);                
            }           
        }
        else{
            System.debug('No Record Found');
        }
    }
 }

Output:

See also:
Conclusion:
Hope you like this tutorial, for any query or suggestions please feel free to comment.
Thank you.