5

特定の販売注文のメモを入力する機能がある 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としてa を設定しています。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ヘッダーでを見つけることができません。

何が間違っているのかわかりません。

4

2 に答える 2

2

@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以下に示すように、ユーザーが電子メールクライアントの返信ボタンをクリックすると、宛先アドレスにアドレスが表示されます。

メール cient の To に表示される返信先アドレス

ユーザーがアドレスを変更しない場合、メールボックス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);

そして、私たちは幸せに暮らしました!!!

于 2014-01-23T07:05:36.087 に答える