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='rijwanmohmmed@gmail.com'];
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {toAddress};
mail.setToAddresses(toAddresses);
// Set Organization-Wide Email Address Id
mail.setOrgWideEmailAddressId(lstEmailAddress[0].Id);
mail.setSubject('Setup OWD Address');
mail.setHtmlBody('This email is for testing purpose of Organization-Wide Email Address');
mails.add(mail);
if(!mails.isEmpty()){
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);
}
}
else{
System.debug('No Record Found');
}
}
}
2 comments
Hi,
I have developed the LWC component in which when user click on Send Email, Email will be send successfully. Now as per Organisation Wide Address Name and Email Id will be displayed as “From”. I have to display Opportunity Owner as from. Can we do this using Apex
Hi @PRANAV Use below one
mail.setSenderDisplayName(‘Opportunity Owner Name’);