Question

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?

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top