发送电子邮件作为回复线程

时间:2016-02-20 09:38:43

标签: email sendgrid

当用户离线且无法实时聊天时,我正在使用sendgrid发送电子邮件

现在的问题是,当我发送电子邮件时,它总是在收件人的电子邮件中创建一个新的“主题”。我想要一个对话线程

我正在使用发送HTTP端点https://sendgrid.com/docs/API_Reference/Web_API/mail.html

有什么想法吗?

1 个答案:

答案 0 :(得分:5)

尝试以下方法:

首先,我假设您使用唯一的会话ID以某种方式跟踪对话?如果没有,请开始这样做。

您需要发送自定义标头:Message-ID,In-Reply-To和References。

下面是一些使用C#和6.3.4 SendGrid nuGet包的示例代码:

using System;
using System.Net;
using System.Net.Mail;
using System.Threading;
using SendGrid;

namespace ConsoleApplication1
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var conversationId = Guid.NewGuid().ToString(); // TO DO: get the real conversation ID from dtaabase

            var testConversationEmailsToSend = 7;

            for (var i = 1; i <= testConversationEmailsToSend; i++)
            {
                var emailNumberForConversation = GetConversationEmailCount(i);
                var messageId = string.Format("{0}-{1}@yourdomain.com", conversationId, emailNumberForConversation);
                var previousMessageId = GetPreviousMessaageId(conversationId, emailNumberForConversation);

                var msg = new SendGridMessage();

                msg.Headers.Add("Message-ID", messageId);
                msg.Headers.Add("In-Reply-To", string.Format("<{0}>", previousMessageId));
                SetReferences(msg, conversationId, emailNumberForConversation);

                msg.AddTo("to@example.com");
                msg.From = new MailAddress("from@example.com");

                msg.Subject = "continuing the conversation";
                msg.Text = string.Format("{0} messaage #{1}", msg.Subject, i);

                var web = new Web(new NetworkCredential("sendgridusername", "sendgridpassword"));
                var task = web.DeliverAsync(msg);

                task.Wait(new TimeSpan(0, 0, 15));

                // sleep 1 minute before sending next email... for testing sample code
                Thread.Sleep(new TimeSpan(0, 0, 15));
            }
        }

        private static object GetPreviousMessaageId(string conversationId, int emailNumberForConversation)
        {
            var previousMessageCount = Math.Max(emailNumberForConversation - 1, 1);

            return string.Format("{0}-{1}@yourdomain.com", conversationId, previousMessageCount);
        }

        private static int GetConversationEmailCount(int dumbyParameterForSampleCode)
        {
            // TO DO: look in database to see how many of these messaages our system has sent.
            // if this is the first email for the conversation we'll return 1;

            return dumbyParameterForSampleCode; // hard setting value only for example code purposes
        }

        private static void SetReferences(SendGridMessage msg, string conversationId, int emailCount)
        {
            var referencesValue = "";

            for (var i = emailCount - 1; i > 0; i--)
            {
                referencesValue += string.Format("<{0}-{1}@yourdomain.com>{2}", conversationId, i, Environment.NewLine);
            }

            msg.Headers.Add("References", referencesValue);
        }
    }
}