1
votes

We are creating an application in .Net and are currently on the final stages and need to inject an email message through SMTP while setting a variety of options in the 'email headers'.

The three values we are focused on are:

  • Return-Path
  • Sender
  • Message-ID
  • Currently in our code we have this:

    BodyMessage.Sender = new MailAddress(bounce_address);
    

    (Where the 'bounce_address' is the address we we want the bounces to go to.)

    This code seems to set both the Return-Path and Sender.

    However, we want those values to be different.

    Is there a value we can use so that when we send the message we can specify the return-path to be one thing and the sender to be another?

    We tried this, but it does not work:

    BodyMessage.Headers.Add("Sender", "[email protected]");
    

    That code just gets completely ignored.

    Any ideas?

    For the message-ID we have tried this:

    BodyMessage.Headers.Add("Message-ID", "[email protected]");
    

    But, that also gets ignored.

    If, however, we specify a custom header value such as "X-Message-ID" then this code works:

    BodyMessage.Headers.Add("X-Message-ID", "[email protected]");
    

    But, that is not what we want since we don't want to add an additional email-header item, but rather supply the main message-id header element.

    Any help is greatly appreciated.

    Thanks in advance!

    1
    Did you have a solution for this? I am looking for a way to set the "return-path" to a different address.dotnetster

    1 Answers

    1
    votes

    When I ran into problems like that (before they introduced ReplyTo collection, it used to be a single item only), I ended up using a 3rd-party SMTP library and stopped using the .NET library altogether. I found out that basically, there's no way to override the default behavior - they specifically prohibit you from settings those headers yourself. (for some reason that I'm failing to grasp)

    DnSmtp

    As the author of that library admits, it's rough on the edges here and there, I had to polish it myself before using it. But I was successful in the end.

    PS. You can use any .NET email library, not necessarily DnSmtp. Or you could write your own SMTP client (a bit tedious, but possible).