Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uniquely identify an email on send using System.Net.Mail

I am looking for a way to uniquely identify an email at the time it is sent using the .NET SmtpClient class (in System.Net.Mail), ie

        SmtpClient client = new SmtpClient(smtpServer, smtpPort);

        client.EnableSsl = false;

        MailAddress from = new MailAddress(fromAddress);
        MailAddress to = new MailAddress(destinationAddress); 

        MailMessage message = new MailMessage(from, to);

        message.Body = body;
        message.Subject = subject;

etc.

I can set the 'Message-Id' property using message.Headers (set it to a System.Guid) but it gets overwritten by the smtp server, and I dont know how to access the message id that the smtp server generates.

I need a client system to open the pop3 mailbox that receives the email and be able to track it down by this unique identifier.

Anyone know if this is possible?

like image 557
Sean Thoman Avatar asked Jun 03 '11 17:06

Sean Thoman


1 Answers

One option is to attach a custom header to the message. Custom headers are indicated with a X- prefix. So in this case, you might create one called X-Unique-Id which would not be overwritten.

However, it's not necessarily the case that all mail clients will allow filtering/searching based on custom headers, you'd have to check the client.

Per MSDN:

Any custom headers added will be included when the MailMessage instance is sent.

Just add to the message.Headers collection like:

message.Headers["X-Unique-Id"] = "1";

Another option would be to use a special reply-to: or from: value that includes the message id. E.g. if your reply address was [email protected], then change it to auto+{unique-id}@responder.net. Many mail servers (including gmail) will treat the + as a wildcard and deliver the message to [email protected] as normal. This is a common approach used for allowing users to reply directly to in-app messages via email.

like image 105
hemp Avatar answered Sep 19 '22 13:09

hemp