简体   繁体   English

使用 System.Net.Mail 在发送时唯一标识 email

[英]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我正在寻找一种方法来在使用 .NET SmtpClient class(在 System.Net.Mail 中)发送时唯一识别 email,即

        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.我可以使用 message.Headers 设置“消息 ID”属性(将其设置为 System.Guid),但它会被 smtp 服务器覆盖,而且我不知道如何访问 smtp 服务器生成的消息 ID。

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.我需要一个客户端系统来打开接收 email 的 pop3 邮箱,并能够通过这个唯一标识符对其进行跟踪。

Anyone know if this is possible?有谁知道这是否可能?

One option is to attach a custom header to the message.一种选择是将自定义 header 附加到消息中。 Custom headers are indicated with a X- prefix.自定义标头用X-前缀表示。 So in this case, you might create one called X-Unique-Id which would not be overwritten.因此,在这种情况下,您可能会创建一个不会被覆盖的名为X-Unique-Id名称。

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 :根据MSDN

Any custom headers added will be included when the MailMessage instance is sent.发送 MailMessage 实例时,将包含添加的任何自定义标头。

Just add to the message.Headers collection like:只需添加到 message.Headers 集合中,例如:

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

Another option would be to use a special reply-to: or from: value that includes the message id.另一种选择是使用包含消息 ID 的特殊回复:或发件人:值。 Eg if your reply address was auto@responder.net, then change it to auto+{unique-id}@responder.net.例如,如果您的回复地址是 auto@responder.net,则将其更改为 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.许多邮件服务器(包括 gmail)会将 + 视为通配符,并将邮件正常发送到 auto@responder.net。 This is a common approach used for allowing users to reply directly to in-app messages via email.这是允许用户通过 email 直接回复应用内消息的常用方法。

Depending on what you mean by "client system", I'd suggest you add a customer header such as X-MyHeader: GUID .根据“客户端系统”的含义,我建议您添加一个客户 header ,例如X-MyHeader: GUID

The X-Headers don't interact with the mail servers; X-Headers不与邮件服务器交互; This is also used by many popular mailing tools, eg mailchimp:这也被许多流行的邮件工具使用,例如 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?您能否将 GUID 附加到主题行或将其添加到 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.用户可以看到它,但如果它位于 email 的底部(可能还有更多信息),它将不碍事,仍然可以进行搜索。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM