C# 如何使用 System.Net.Mail 设置 SMTP 信封 MAIL FROM?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51793/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can you set the SMTP envelope MAIL FROM using System.Net.Mail?
提问by Eric Z Beard
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?
当您使用 C# 和 System.Net.Mail 命名空间发送电子邮件时,您可以在 MailMessage 对象上设置“发件人”和“发件人”属性,但这些属性都不允许您创建 MAIL FROM 和发件人地址进入彼此不同的 DATA 部分。MAIL FROM 设置为“From”属性值,如果您设置“Sender”,它只会在 DATA 部分添加另一个标题字段。这导致“来自 [email protected] 代表 [email protected]”,这不是您想要的。我错过了什么吗?
The use case is controlling the NDR destination for newsletters, etc., that are sent on behalf of someone else.
用例是控制代表其他人发送的时事通讯等的 NDR 目的地。
I am currently using aspNetEmailinstead 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 属性完成的。
回答by George Mauer
Do you mean this?:
你是这个意思吗:
//create the mail message
MailMessage mail = new MailMessage();
//set the addresses
mail.From = new MailAddress("[email protected]");
mail.To.Add("[email protected]");
//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);
回答by Romhein
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
- mail.From 指定最终用户可见的电子邮件
- mail.Sender 指定信封 MAIL FROM
That's it (even if it took me a while to figure it out)
就是这样(即使我花了一段时间才弄明白)
回答by bzlm
MailMessage.Sender
will always insert a Sender
header (interpreted as on behalf ofin 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.
如果使用 上的Network
递送方式SmtpClient
,.Sender
也会更改信封中的发件人。使用PickupDirectoryFromIis
传递方法将留给 IIS 来确定信封发件人,IIS 将使用From
地址,而不是Sender
地址。
回答by Corben Leek
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 protected]"
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
如果您点击回复电子邮件设置为回复地址