发送电子邮件作为回复主题 [英] Send email as reply to thread

查看:185
本文介绍了发送电子邮件作为回复主题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using sendgrid to send emails when users are offline and cannot chat in realtime

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

Problem that right now, when I send an email, it is always creating a new 'thread' in the recipient's email. I want to have a conversation thread

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

I am using the Send HTTP endpoint https://sendgrid.com/docs/API_Reference/Web_API/mail.html

有什么想法吗?

推荐答案

尝试以下操作:

首先,我假设您正在跟踪对话(s)以某种方式使用唯一的对话ID?如果没有,请开始执行此操作。

First, I assume you are tracking the conversation(s) somehow using a unique conversation id? If not, start doing this.

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

You'll want to send in custom headers: Message-ID, In-Reply-To, and References.

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

Below is some sample code using C# and the 6.3.4 SendGrid nuGet Package:

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);
        }
    }
}

这篇关于发送电子邮件作为回复主题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆