如何使用System.Net.Mail设置SMTP信封MAIL FROM?

当您使用C#和System.Net.Mail命名空间发送电子邮件时,您可以在MailMessage对象上设置“发件人”和“发件人”属性,但这些都不允许您生成MAIL FROM和来自的地址进入DATA部分彼此不同。 MAIL FROM设置为“From”属性值,如果设置“Sender”,它只在DATA部分添加另一个头字段。 这导致“来自X@Y.COM代表A@B.COM”,这不是您想要的。 我错过了什么吗?

用例是控制代表其他人发送的新闻稿等的NDR目的地。

我目前正在使用aspNetEmail而不是System.Net.Mail,因为它允许我正确地执行此操作(与大多数其他SMTP库一样)。 使用aspNetEmail,这是使用EmailMessage.ReversePath属性完成的。

MailMessage.Sender将始终插入Sender标头( 代表您的电子邮件客户端解释)。

如果您在SmtpClient上使用Network传送方法, .Sender也会更改信封中的发件人。 使用PickupDirectoryFromIis传递方法将其留给IIS以确定信封发件人,IIS将使用From地址,而不是Sender地址。

这里有关于MSDN的类似问题。

我刚刚发现了怎么做:

  • mail.From指定从可见到最终用户的电子邮件
  • mail.Sender指定信封MAIL FROM

就是这样(即使我花了一段时间来弄明白)

如果添加以下行,则会在邮件头中设置Return-Path和Reply-To标头。

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

如果您点击回复设置为回复地址的电子邮件

你是说这个吗?:

 //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. This is bold This is blue"; mail.IsBodyHtml = true; //send the message SmtpClient smtp = new SmtpClient("127.0.0.1"); smtp.Send(mail); 

来自http://www.systemnetmail.com/faq/3.1.2.aspx