How can you set the SMTP envelope MAIL FROM using

2019-02-03 09:41发布

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 X@Y.COM on behalf of A@B.COM", 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.

标签: c# .net smtp
4条回答
倾城 Initia
2楼-- · 2019-02-03 09:57

Do you mean this?:

//create the mail message
 MailMessage mail = new MailMessage();

 //set the addresses
 mail.From = new MailAddress("me@mycompany.com");
 mail.To.Add("you@yourcompany.com");

 //set the content
 mail.Subject = "This is an email";
 mail.Body = "this is a sample body with html in it. <b>This is bold</b> <font color=#336699>This is blue</font>";
 mail.IsBodyHtml = true;

 //send the message
 SmtpClient smtp = new SmtpClient("127.0.0.1");
 smtp.Send(mail);

From http://www.systemnetmail.com/faq/3.1.2.aspx

查看更多
对你真心纯属浪费
3楼-- · 2019-02-03 09:58

If you add the following lines the Return-Path and the Reply-To headers are set in the mail header.

Dim strReplyTo As String = "email@domain.tld"
message.ReplyToList.Add(strReplyTo)
message.Headers.Add("Return-Path", strReplyTo)

And if you click on reply the e-mail set to the Reply-To address

查看更多
等我变得足够好
4楼-- · 2019-02-03 10:02

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.

查看更多
孤傲高冷的网名
5楼-- · 2019-02-03 10:11

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)

查看更多
登录 后发表回答