FAQ
overflow

Great Answers to
Questions About Everything

QUESTION

We are using the Single email message class to send emails on a forgot user name scenario. We want to change the from address display name to a valid name. I used the email.setSenderDisplayName method and override the name and still salesforce is overriding it with a custom name. How do we set the display name to a valid name? This is happening on a force.com site and for the guest user profile. I could not use the organization wide email address as salesforce does not show the guest user profile on the drop down.

here is the code Messaging.Singleemailmessage email = new Messaging.Singleemailmessage();

        email.setReplyTo(fromaddress);
         orgName = UserInfo.getOrganizationName();
        email.setSenderDisplayName(orgName);
        apexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'sender display name'+ String.valueOf(orgName ) ));
        email.setToAddresses(new String[] { emailTo });

        email.setSubject(subject);
        email.setHtmlBody(htmlBody);
        email.setPlainTextBody(plainBody);

        Messaging.sendEmail(new Messaging.SingleEmailmessage[] {email});

{ asked by buyan kumar }

ANSWER

I fixed this. The solution is that to create an organization wide email address and set it to all profiles which would work for guest user profile as well. On the code , just call the following

List<OrgWideEmailAddress> lstOrgWideEmailId = [Select id from OrgWideEmailAddress];
if(lstOrgWideEmailId.size() == 0)
{
   throw(new PkException('There is no Organization wide email address setup in the org. Please set the organization wide email address'));
}
else
{
    orgWideEmailAddressID = lstOrgWideEmailId[0].id;    
}
email.setOrgWideEmailAddressId(orgWideEmailAddressID);

This will help to solve issues in future.

{ answered by buyan kumar }
Tweet