[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.

Exception:

 SendEmail failed. First exception on row 0; first error:
        DUPLICATE_SENDER_DISPLAY_NAME, 
        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.setOrgWideEmailAddressId(owe.Id);
            conformemail.setHtmlBody('Demo Body');
            conformemail.setReplyTo('demo@gmail.com'); 
            conformemail.setSenderDisplayName('Ramesh');                 
            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:

conformemail.setSenderDisplayName('Ramesh');
Related Topic