如何知道哪个电子邮件是对C#中另一封电子邮件的回复?

时间:2012-02-29 06:28:25

标签: c# asp.net email openpop

我开发了一个Web应用程序,其中有一个功能可以输入特定销售订单的备注。

当客户或客户服务主管输入注释时,会向相关方发送电子邮件通知(使用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("replyto@email.com");                    
    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

这一切都很好。

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

我想在同一Sales -Order的注释中记录回复(对方收到通知)。

我正在使用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;
    }
}

通过上述功能,我可以阅读我的“replyto@email.com”帐户中的所有电子邮件。但我无法在电子邮件的Message-Id标题中找到In-reply-to

我不知道我做错了什么。

2 个答案:

答案 0 :(得分:3)

我能想到的最佳解决方案是将您的数据放在“发件人”和/或“回复”标题中,例如使用“+”符号。

说你的回复地址是replies@yourdomain.com

您必须在邮件服务器中添加过滤规则,以便发送到回复+anyrelevantdatahere@yourdomain.com的任何邮件都包含在replies@yourdomain.com邮箱中

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

gmail也使用它(如果你有一个gmail地址就试试)

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

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

答案 1 :(得分:1)

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

当发送任何电子邮件时,我们会对下面的订单中输入的每个注释做出唯一的回复。

String sReplyToadd = "replyto@domain.com";
String replyToAddress = sReplyToadd.Substring(0, sReplyToadd.IndexOf('@')) + "+on" + orderID + "un" + userID + sReplyToadd.Substring(sReplyToadd.IndexOf('@'), sReplyToadd.Length - sReplyToadd.IndexOf('@'));

这将使replyToAddress = "replyto+on1234un5678@domain.com"成为识别订单的唯一地址以及发布该笔记的用户。

现在,此地址的唯一回复将分配给如下所示的电子邮件。

using (MailMessage objEmail = new MailMessage())
{
    objEmail.Body = eMailBody;
    objEmail.From = new MailAddress("from@domain.com", "Display Name");
    objEmail.IsBodyHtml = true;
    objEmail.Subject = "email subject goes here";
    objEmail.To.Add(new MailAddress("tosomeuser@gmail.com");

    //here we set the unique reply to address for the outgoing email
    objEmail.ReplyTo = new MailAddress(replyToAddress); //replyto+on1234un5678@domain.com

    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地址,则会在我们的replyto@domain.com邮箱中收到该地址。我们在每封电子邮件的底部都写了一个注释:请不要更改收件人地址,以便将您的回复正确映射到系统。

电子邮件到达邮箱后,我们只需要查看电子邮件回复的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);

然后我们过着幸福的生活!