开发者

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

开发者 https://www.devze.com 2023-03-10 10:54 出处:网络
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

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?


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 auto@responder.net, 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 auto@responder.net as normal. This is a common approach used for allowing users to reply directly to in-app messages via email.


Depending on what you mean by "client system", I'd suggest you add a customer header such as X-MyHeader: GUID.

The X-Headers don't interact with the mail servers; This is also used by many popular mailing tools, e.g. mailchimp:

System.Net.Mail.MailMessage m;
m.Headers.Add("X-MyHeader", Guid.NewGuid());


Can you attached a GUID to the Subject line or add it to the very end of the body of the email? It would be visible to the user but if it is at the bottom of the email (perhaps with some more information) it would be out of the way and would still make searching possible.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号