Implementation of the mailing list in .NET.

I am using a mailing list using .NET. As discussed in this answer , I need to send an email where the recipient of the envelope differs from the recipient in the To header. How to achieve this in C #? The SmtpClient and MailMessage classes in System.Net.Mail do not seem to allow this.

I tried:

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

but mail is not sent to what is indicated in the envelope.

Any suggestions?

+4
source share
1 answer

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

You can use the MailMessage.Bcc property. Addresses added there will only be displayed in Envelope-To , and not in To mail:

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

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

Here I am absolutely sure that you are out of luck. I looked at the System.Net.Mail namespace using ILSpy and it seems like this is not possible. The To header of the mail is created from the To MailMessage property (see Message.PrepareHeaders), and the same property is used to fill the Envelope-To mail (along with Cc and Bcc , see SmtpClient.Send). Manually setting Headers["To"] will not help, since this value is overwritten by the contents of the To property (see Message.PrepareHeaders).

So, list@example.com will receive a copy of the message. Depending on the configuration of your SMTP server, this may result in a mail cycle.

+3
source

Source: https://habr.com/ru/post/1402766/


All Articles