C# 使用 Windows 虚拟邮件服务器发送带有标题返回路径的电子邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/464876/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 04:27:39  来源:igfitidea点击:

Sending an email with the header return-path using windows virtual mail server

c#emailmailmessagereturn-path

提问by John_

I'm trying to send an email message using the .NET MailMessage class which can also have the return-path header added so that any bounces come back to a different email address. Code is below:

我正在尝试使用 .NET MailMessage 类发送电子邮件,该类还可以添加返回路径标头,以便任何退回邮件都返回到不同的电子邮件地址。代码如下:

MailMessage mm = new MailMessage(
    new MailAddress(string.Format("{0}<{1}>", email.FromName, email.FromEmail)), 
    new MailAddress(emailTo));

mm.Subject = ReplaceValues(email.Subject, nameValues);
mm.ReplyTo = new MailAddress(string.Format("{0}<{1}>", email.FromName, email.FromEmail));
mm.Headers.Add("Return-Path", ReturnEmail);

// Set the email html and plain text
// Removed because it is unneccsary for this example

// Now setup the smtp server
SmtpClient smtp = new SmtpClient();
smtp.Host = SmtpServer;
smtp.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;

if (SmtpUsername.Length > 0)
{
    System.Net.NetworkCredential theCredential = 
        new System.Net.NetworkCredential(SmtpUsername, SmtpPassword);
    smtp.Credentials = theCredential;
}

smtp.Send(mm);

Whenever I check the email that was sent I check the header and it always seems to be missing return-path. Is there something I am missing to configure this correctly? As I said above I'm using the standard Virtual Mail Server on my development machine (XP) however it will run on Windows 2003 eventually.

每当我检查发送的电子邮件时,我都会检查标题,但它似乎总是缺少返回路径。我是否缺少正确配置它的东西?正如我上面所说,我在我的开发机器 (XP) 上使用标准虚拟邮件服务器,但它最终将在 Windows 2003 上运行。

Has anyone got any ideas why it isn't coming through?

有没有人知道为什么它没有通过?

采纳答案by Romhein

The Return-Path is set based on the SMTP MAIL FROM Envelope. You can use the Sender property to do such a thing.
Another discussion on a related issue you will have sooner or later: How can you set the SMTP envelope MAIL FROM using System.Net.Mail?

返回路径是根据 SMTP MAIL FROM Envelope 设置的。您可以使用 Sender 属性来做这样的事情。
关于您迟早会遇到的相关问题的另一个讨论:如何使用 System.Net.Mail 设置 SMTP 信封 MAIL FROM?

And btw, if you use SmtpDeliveryMethod.PickupDirectoryFromIis, the Sender property is not used as a MAIL FROM; you have to use Network as a delivery method to keep this value. I did not find any workaround for this issue.
PickupDirectoryFromIis, Sender property and SMTP MAIL FROM envelope

顺便说一句,如果您使用 SmtpDeliveryMethod.PickupDirectoryFromIis,则 Sender 属性不会用作 MAIL FROM;您必须使用 Network 作为交付方式来保持此值。我没有找到任何解决此问题的方法。
PickupDirectoryFromIis、Sender 属性和 SMTP MAIL FROM 信封