views:

1295

answers:

1

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  A: 

Hi John_,

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: http://stackoverflow.com/questions/51793/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.
http://stackoverflow.com/questions/457880/pickupdirectoryfromiis-sender-property-and-smtp-mail-from-envelope

Nip
Sorry for the slow reply I had completely forgotten about this as another project came up. Thanks for your help it has indeed solved my problem.
John_