4

I am implementing a mailing list using using .NET. As discussed in this answer, I need to send an email where the recipient of the envelope is different from the recipient in the To header. How to achieve this in C#? The SmtpClient and MailMessage classes in System.Net.Mail doens't seem to permit this.

I tried:

        message.To.Add("list@example.com");
        message.Headers["Envelope-to"] = "user@example.com";

but the mail doesn't get sent to what it is specified in the Envelope-to.

Any suggestions?

Community
  • 1
  • 1
nakhli
  • 4,009
  • 5
  • 38
  • 61

1 Answers1

3

Adding an address to Envelope-To without adding it to To

You can use the MailMessage.Bcc property. Addresses added there will only appear in the Envelope-To, not in the mail's To:

message.Bcc.Add("user@example.com");

Adding an address to To without adding it to Envelope-To

Here, I'm quite sure you are out of luck. I've had a look at the System.Net.Mail namespace with ILSpy, and it looks like this is not possible. The To header of the mail is created out of the To property of the MailMessage (see Message.PrepareHeaders), and the same property is used to fill the Envelope-To of the mail (together with the Cc and Bcc properties, see SmtpClient.Send). Manually setting Headers["To"] won't help, since this value is overwritten with the contents of the To property (see Message.PrepareHeaders).

So, list@example.com will get a copy of the message. Depending on the configuration of your SMTP server, this might lead to a mail loop.

Heinzi
  • 167,459
  • 57
  • 363
  • 519
  • Thank you for the detailed answer. Is there any third party smtp client which can achieve this? – nakhli Mar 22 '12 at 09:00
  • @ChakerNakhli: Unfortunately, I don't know of any. If you don't find one with Google, it might make sense to start a new question on that. – Heinzi Mar 22 '12 at 09:48
  • Yuck. Definitely not what is desired. Use some other library as suggested [here](http://stackoverflow.com/a/165403/201725), but unfortunately that suggestion is not free. It would be strange that there were no free libraries for this. – Jan Hudec Mar 13 '13 at 15:36