Search code examples
c#emailreturn-path

Sending Email with return-path not functioning


I am using System.Net.Mail email. in the code i am setting the return-path of email as follow:

string sReturnPath = ConfigurationManager.AppSettings["ReturnPath"].ToString();
if (sReturnPath.Length > 0) {
    msg.Headers.Add("Return-Path", sReturnPath);
}

If the delivery has failed it should go to return-path but it doesn't, even though I can see the header of email's return-path being from config file that I specified. The email gets returned to sender.

any ideas?


Solution

  • You're using a slightly wrong approach. Simple Mail Transfer Protocol (RFC 2821) says:

    A message-originating SMTP system SHOULD NOT send a message that
    already contains a Return-path header. SMTP servers performing a
    relay function MUST NOT inspect the message data, and especially not
    to the extent needed to determine if Return-path headers are present.

    If you want your message to be returned to the address other than the one specified in the From: field, then the address should be set in the SMTP message envelope rather than in the message header. You can achieve this by setting the Sender property:

    MailMessage msg = new MailMessage();
    msg.Sender = new MailAddress("[email protected]", "Sam1");