如何知道哪些电子邮件是对C#中的其他电子邮件的回复?

问题描述:

我开发了一个Web应用程序,其中有一个功能可以输入特定销售订单的注释。如何知道哪些电子邮件是对C#中的其他电子邮件的回复?

当客户或客户服务主任输入一条便条时,会向相关方发送电子邮件通知(使用C#中的SmtpClient & MailMessage对象发送电子邮件通知)。

using (MailMessage objEmail = new MailMessage()) 
{ 
    Guid objGuid = new Guid(); 
    objGuid = Guid.NewGuid(); 
    String MessageID = "<" + objGuid.ToString() + ">"; 
    objEmail.Body = messagebody.ToString(); 
    objEmail.From = new MailAddress(sFrmadd, sFrmname); 
    objEmail.Headers.Add("Message-Id", MessageID); 
    objEmail.IsBodyHtml = true; 
    objEmail.ReplyTo = new MailAddress("[email protected]");      
    objEmail.Subject = sSubject;      
    objEmail.To.Add(new MailAddress(sToadd)); 

    SmtpClient objSmtp = new SmtpClient(); 
    objSmtp.Credentials = new NetworkCredential("mynetworkcredential", "mypassword"); 
    objSmtp.DeliveryMethod = SmtpDeliveryMethod.Network; 
    objSmtp.EnableSsl = true; 
    objSmtp.Host = "myhostname"; 
    objSmtp.Port = 25; 
    objSmtp.Timeout = 3 * 3600; 

    objSmtp.Send(objEmail);      
} 

我设置一个Guid作为消息的Message-Id在消息报头被发送。

这一切工作正常。

现在我想开发一个功能,让各方从各自的收件箱中回复电子邮件通知。

而且我想在相同销售订单的注释中记录答复(对方为其收到通知)。

我正在使用OpenPop.dll来阅读通知回复的收件箱。

/// <summary> 
/// Fetch all messages from a POP3 server 
/// </summary> 
/// <param name="hostname">Hostname of the server. For example: pop3.live.com</param> 
/// <param name="port">Host port to connect to. Normally: 110 for plain POP3, 995 for SSL POP3</param> 
/// <param name="useSsl">Whether or not to use SSL to connect to server</param> 
/// <param name="username">Username of the user on the server</param> 
/// <param name="password">Password of the user on the server</param> 
/// <returns>All Messages on the POP3 server</returns> 
public static List<Message> FetchAllMessages(string hostname, int port, bool useSsl, string username, string password) 
{ 
    // The client disconnects from the server when being disposed 
    using (Pop3Client client = new Pop3Client()) 
    { 
     // Connect to the server 
     client.Connect(hostname, port, useSsl); 

     // Authenticate ourselves towards the server 
     client.Authenticate(username, password); 

     // Get the number of messages in the inbox 
     int messageCount = client.GetMessageCount(); 

     // We want to download all messages 
     List<Message> allMessages = new List<Message>(messageCount); 

     // Messages are numbered in the interval: [1, messageCount] 
     // Ergo: message numbers are 1-based. 
     for (int i = 1; i <= messageCount; i++) 
     { 
      allMessages.Add(client.GetMessage(i)); 
     } 

     // Now return the fetched messages 
     return allMessages; 
    } 
} 

从上面的函数中,我可以读取来自我的“[email protected]”帐户的所有电子邮件。但我无法在电子邮件的In-reply-to标题中找到Message-Id

我不知道我在做什么错。

+1

据我所知没有可靠的方法来做到这一点,你是在一个电子邮件客户端的摆布,每个电子邮件客户端有它自己的怪癖。我见过的大多数类似系统都使用*主题行*来放置一个唯一的ID,然后,当邮件被回复以从头开始剥离“RE:”时。 – 2012-02-29 06:39:50

+0

我们在电子邮件中添加了一些自定义标题。 – PraveenVenu 2012-02-29 07:18:39

+0

我相信即使Outlook的对话也是基于主题的。 – 2012-02-29 08:46:33

我能想到的最佳解决方案是使用例如'+'标记将数据放入“From”和/或“Reply-to”标题中。

说你的回报是ADRESS [email protected]

您必须添加一个过滤规则在你的邮件服务器,使发送到[email protected]任何消息落入[email protected]邮箱

Facebook通知将其用于直接电子邮件回复。

Gmail使用它太(试试吧,如果你有一个Gmail地址)

(见http://forums.smartertools.com/showthread.php/27790-Plus-Addressing-configure-symbol

希望这会有所帮助。如果是这样,邮件服务器配置好运

正如@jbl所回答的,我们使用了加号寻址的概念。我们要求我们的电子邮件提供商在我们的SMTP服务器上启用此概念。 Gmail默认提供此功能。

在发送任何电子邮件时,我们会针对在下面的订单中输入的每个备注的地址作出唯一回复。

String sReplyToadd = "[email protected]"; 
String replyToAddress = sReplyToadd.Substring(0, sReplyToadd.IndexOf('@')) + "+on" + orderID + "un" + userID + sReplyToadd.Substring(sReplyToadd.IndexOf('@'), sReplyToadd.Length - sReplyToadd.IndexOf('@')); 

这将使replyToAddress = "[email protected]",一个唯一的地址来标识订单和发布笔记的用户。

现在,这个唯一的地址回复将被分配给下面发送的电子邮件。

using (MailMessage objEmail = new MailMessage()) 
{ 
    objEmail.Body = eMailBody; 
    objEmail.From = new MailAddress("[email protected]", "Display Name"); 
    objEmail.IsBodyHtml = true; 
    objEmail.Subject = "email subject goes here"; 
    objEmail.To.Add(new MailAddress("[email protected]"); 

    //here we set the unique reply to address for the outgoing email 
    objEmail.ReplyTo = new MailAddress(replyToAddress); //[email protected] 

    SmtpClient objSmtp = new SmtpClient(); 

    objSmtp.EnableSsl = true; 
    objSmtp.Credentials = new NetworkCredential("username", "password"); 
    objSmtp.Host = "127.0.0.1";//"smtp.gmail.com" for gmail 
    objSmtp.Port = 25; 

    objSmtp.Send(objEmail); 
} 

ReplyTo地址将出现在处理,如果用户,如下图所示在其电子邮件客户端点击回复按钮。

Reply To Address shown in To in the email cient

如果用户不改变To地址,它会在我们的[email protected]邮箱接收。我们在每封电子邮件的底部写下一个注释:请不要更改收件人地址以正确地将您的答复映射到系统。

一旦邮件到达邮箱,我们只需要检查电子邮件回复的To地址,并获得所需的顺序ID和用户ID,如下

String replyFor = objEmail.To[0].ToString(); 
Int64 orderID = Convert.ToInt64(replyFor.Substring(replyFor.LastIndexOf("+on") + 3, replyFor.LastIndexOf("un"))); 
Int64 userID = replyFor.Substring(replyFor.LastIndexOf("un") + 2, replyFor.IndexOf("@") - replyFor.LastIndexOf("un") - 2); 

然后我们过得很幸福!