2016-02-20 140 views

回答

4

嘗試以下操作:

首先,我假設你正在跟蹤莫名其妙地採用了獨特的對話id的對話(S)?如果沒有,開始這樣做。

你會想在自定義首派:郵件ID,在-回覆地址,和參考。

下面是一些示例代碼使用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("[email protected]"); 
       msg.From = new MailAddress("[email protected]"); 

       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); 
     } 
    } 
} 
相關問題