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

时间:2022-12-04 08:20:31

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?

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

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

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

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.

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

4 个解决方案

#1


7  

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

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

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.

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

There's a similar question on MSDN here.

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

#2


4  

I just found how to do it:

我刚刚发现了怎么做:

  • mail.From specify the email from visible to the final user
  • mail.From指定从可见到最终用户的电子邮件

  • mail.Sender specifies the envelope MAIL FROM
  • mail.Sender指定信封MAIL FROM

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

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

#3


2  

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

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

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


1  

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

#1


7  

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

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

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.

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

There's a similar question on MSDN here.

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

#2


4  

I just found how to do it:

我刚刚发现了怎么做:

  • mail.From specify the email from visible to the final user
  • mail.From指定从可见到最终用户的电子邮件

  • mail.Sender specifies the envelope MAIL FROM
  • mail.Sender指定信封MAIL FROM

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

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

#3


2  

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

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

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


1  

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