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);
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:
-
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.
-
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