1

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?

1 Answer 1

13

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");
7
  • 1
    i just noticed something if you send it using sender then the email would say "on behalf of" any solutions for this
    – Zaki
    Commented Jan 19, 2012 at 16:07
  • Where exactly would it say "on behalf of"? Commented Jan 19, 2012 at 16:27
  • on the from address it says [email protected]; on behalf of; [email protected]... that is when the email is sent successfully
    – Zaki
    Commented Jan 19, 2012 at 16:40
  • Well, my guess is that the destination SMTP server adds this string to a message header, I don't think you can do much about it. Commented Jan 19, 2012 at 16:57
  • is there any way i can disable this in smtp server? as msg.sender does the bounce back job for me the only problem is the behalf thing...
    – Zaki
    Commented Jan 19, 2012 at 16:59

Not the answer you're looking for? Browse other questions tagged or ask your own question.