[SalesForce] How to override the FROM Email in Messaging.SingleEmailMessage

How to override the from email address in the email.But in my code throw an exception.How to set the Organization-Wide email addresses.When I comment the code "setOrgWideEmailAddressId(owe.Id);"it send an email but From email address still displayed in email.


 SendEmail failed. First exception on row 0; first error:
        When an Org-Wide Email Address ID is specified, 
        a sender display name may not be specified.: []

I am unable to override the FROM EMAIL address. Some one can please help how to resolve the issue?

Actual code

OrgWideEmailAddress owe = [SELECT ID,IsAllowAllProfiles,DisplayName,Address FROM OrgWideEmailAddress WHERE IsAllowAllProfiles = TRUE LIMIT 1];

Messaging.SingleEmailMessage conformemail = NEW Messaging.SingleEmailMessage();
            conformemail.setSubject('Demo Subject');
            conformemail.setHtmlBody('Demo Body');
            conformemail.setToAddresses(NEW String[]{'ramesh@gmail.com'});               
            Messaging.sendEmail(NEW Messaging.SingleEmailMessage[]{conformemail}); 

Best Answer

As @crop1645 mentioned, the error message is fairly clear in this case. When you specify the Org-Wide Email Address you're sending the message from, you will not be able to also set the Sender Display Name. Your code example should work correctly after you remove this line:

Related Topic