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

我的未来我决定 提交于 2019-12-19 09:48:06

问题


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:

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.

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


回答1:


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?

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



来源:https://stackoverflow.com/questions/464876/sending-an-email-with-the-header-return-path-using-windows-virtual-mail-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!