Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you set the SMTP envelope MAIL FROM using System.Net.Mail?

Tags:

c#

.net

smtp

When you send an email using C# and the System.Net.Mail namespace, you can set the "From" and "Sender" properties on the MailMessage object, but neither of these allows you to make the MAIL FROM and the from address that goes into the DATA section different from each other. MAIL FROM gets set to the "From" property value, and if you set "Sender" it only adds another header field in the DATA section. This results in "From [email protected] on behalf of [email protected]", which is not what you want. Am I missing something?

The use case is controlling the NDR destination for newsletters, etc., that are sent on behalf of someone else.

I am currently using aspNetEmail instead of System.Net.Mail, since it allows me to do this properly (like most other SMTP libraries). With aspNetEmail, this is accomplished using the EmailMessage.ReversePath property.

like image 850
Eric Z Beard Avatar asked Sep 09 '08 12:09

Eric Z Beard


People also ask

What is the SMTP envelope?

SMTP (or Simple Mail Transfer Protocol) is the communication protocol for email transmission. SMTP envelopes tell email servers where to send the emails to, where email clients read the email headers and body to display to the user.

What is System Net mail?

Allows applications to send email by using the Simple Mail Transfer Protocol (SMTP). The SmtpClient type is obsolete on some platforms and not recommended on others; for more information, see the Remarks section.

Where is System Net mail?

The System. Net.Mail classes are contained in the "System. dll" file. Make sure you have a reference to System listed under References in your project.

What is Net mail SmtpClient?

This class allows you to attach files, streams, or text to an email message. MailAddress. Represents the email address of the sender and recipients. MailMessage. Represents an email message.


2 Answers

MailMessage.Sender will always insert a Sender header (interpreted as on behalf of in your e-mail client).

If you use the Network delivery method on the SmtpClient, .Sender will also change the sender in the envelope. Using the PickupDirectoryFromIis delivery method will leave it to IIS to determine the envelope sender, and IIS will use the From address, not the Sender address.

There's a similar question on MSDN here.

like image 191
bzlm Avatar answered Oct 26 '22 14:10

bzlm


I just found how to do it:

  • mail.From specify the email from visible to the final user
  • mail.Sender specifies the envelope MAIL FROM

That's it (even if it took me a while to figure it out)

like image 36
Romhein Avatar answered Oct 26 '22 14:10

Romhein