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?
Adding an address to
Envelope-Towithout adding it toToYou can use the MailMessage.Bcc property. Addresses added there will only appear in the
Envelope-To, not in the mail’sTo:Adding an address to
Towithout adding it toEnvelope-ToHere, 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
Toheader of the mail is created out of theToproperty of the MailMessage (see Message.PrepareHeaders), and the same property is used to fill theEnvelope-Toof the mail (together with theCcandBccproperties, see SmtpClient.Send). Manually settingHeaders["To"]won’t help, since this value is overwritten with the contents of theToproperty (see Message.PrepareHeaders).So,
list@example.comwill get a copy of the message. Depending on the configuration of your SMTP server, this might lead to a mail loop.