0

I wanted to send an email from my Apex class, and I used the following code:

Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
message.toAddresses = new String[] { emailId };
message.optOutPolicy = 'FILTER';
message.setSenderDisplayName(senderDisplay);
message.subject = emailSubject;
message.setHtmlBody(emailFinalBody);
Messaging.SingleEmailMessage[] messages = new List<Messaging.SingleEmailMessage> {message};
Messaging.SendEmailResult[] results = Messaging.sendEmail(messages);

And, this is the mail that the customer gets:enter image description here As you can see from the code, i have added setSenderDisplayName(senderDisplay) string while sending the mail, but in the received mail, sender is <mail id of the org logged in user> via <a url>, and I have to go to the details of the mail to see the senderDisplay. Also, the subject emailSubject Educational content from SampleAddress, is prefixed with <some code> Sandbox:, on the mail. So, is there any way to remove all these things? Is there any way to send the mail as it contains only the things that I specified while sending it?

  • The code + 'Sandbox' just tells you that the email is from a Sandbox. When emails are sent from a production org, this is not there. – Florian Heer May 13 at 11:28
  • K.. thanks for the comment. but, what about the senderDisplayName? – Jayesh Babu May 13 at 11:31
4

Florian's comment explains that the "Sandbox" prefix will disappear in Production.

For the senderDisplayName and mail id of the user showing, what's the value of what you're passing into setSenderDisplayname()? I copied and pasted your code and was able to see it display correctly. Be aware it's still using the user's email address from Salesforce to send from, it's just the display name that's changing.

Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
message.toAddresses = new String[] { 'your test email here' };
message.optOutPolicy = 'FILTER';
message.setSenderDisplayName('Testing Display Name');
message.subject = 'This is my test';
message.setHtmlBody('emailFinalBody');
Messaging.SingleEmailMessage[] messages = new List<Messaging.SingleEmailMessage> {message};
Messaging.SendEmailResult[] results = Messaging.sendEmail(messages);

enter image description here

For the via url part, this knowledge article explains why this is a gmail issue and not a Salesforce issue.

This is a security feature set by Gmail which displays the full email address of the sender as the name doesn't match.

I would test in Outlook or another service to confirm it looks as you want as well. For getting rid of the gmail part, I would defer to the article for the two options below:

  1. As mentioned in the Google article following a best practice of using DKIM signing Create a DKIM Key in Salesforce combined with an SPF Sender Policy Framework (Sender Policy Framework (SPF) and Salesforce SPF Records) entry for Salesforce MTA IPs in the senders DNS should resolve the problem.

  2. Another alternative could be to disable following email deliverability settings in Salesforce to remove the *bnc.salesforce.com from the sender domain.

    -Activate bounce management

    -Enable compliance with standard email security mechanisms

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.