Setup Organization-Wide Address in Salesforce Apex

by Rijwan Mohmmed
0 comment

Hello friends, today 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 we will use our Organization-Wide Address in Salesforce email class.

Setup Organization-Wide Address in Salesforce

navigating to Setup > Administration Setup > Email > Organization-Wide Addresses.

Step 1. Click on Add button. and fill Display Name and Email Address

Also we can select all profile and particular profile that specify who can use this Organization-Wide Address .

Step 2. Click Save button and a verification email sent to you , open the mail and click on link for verify.

Step 3. now navigating to Setup > Administration Setup > Email > Organization-Wide Addresses and you can see email is verified.

Now we can use this in apex class lets see in simple example

public class OrgWideEmailAddressHandler { 
    public void SendEmail(String toAddress){  
        List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
        // Query from Organization-Wide Email Address       
        List<OrgWideEmailAddress> owdEmail =[select Id from OrgWideEmailAddress WHERE Address=''];
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();   
        String[] toAddresses = new String[] {toAddress}; 
        // Set Organization-Wide Email Address Id
        mail.setSubject('Setup OWD Address'); 
        mail.setHtmlBody('This email is for testing purpose of Organization-Wide Email Address');
            Messaging.SendEmailResult[] results = Messaging.sendEmail(mails);
            if (results[0].success) {
                System.debug('The email was sent successfully.');                
            else {
                System.debug('The email failed to send: ' + results[0].errors[0].message);                
            System.debug('No Record Found');

You may also like

Leave a Comment