根据消息ID查询SendGrid API

时间:2019-07-03 17:17:15

标签: c# sendgrid sendgrid-api-v3

因此,我将移至SendGrid以使用其电子邮件服务,并且在发送电子邮件时,如果电子邮件成功排队,则响应对象将创建X-Message-Id。但这不算退回电子邮件。

static async Task SendEmail()
{
    var apiKey = ConfigurationManager.AppSettings["apiKey"];
    var client = new SendGridClient(apiKey);
    var from = new EmailAddress("test@example.com", "Example User");
    var subject = "Hello World from the Twilio SendGrid CSharp Library Helper!";
    var to = new EmailAddress("testUser@recipient.com", "Jeeno");
    var plainTextContent = "Hello, Email from the helper [SendSingleEmailAsync]!";
    var htmlContent = "<strong>Hello, Email from the helper! [SendSingleEmailAsync]</strong>";
    var msg = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, htmlContent);

    var response = await client.SendEmailAsync(msg);
    Console.WriteLine(msg.Serialize());
    Console.WriteLine("============================================1");
    Console.WriteLine(response.StatusCode);
    Console.WriteLine("============================================2");
    Console.WriteLine(response.Headers);
    Console.WriteLine("============================================3");
    Console.WriteLine("\n\nPress <Enter> to continue.");
    Console.ReadLine();
}

我想创建一个计划任务,然后查询所有退回的电子邮件,并根据该ID或实际上任何ID将其退回。但是,当单击sendgrip api终结点以退回电子邮件时,我只得到带有时间戳,电子邮件字符串,原因字符串和状态字符串的响应对象。没有办法根据消息ID进行查询吗?

static async Task Execute()
{
    var apiKey = ConfigurationManager.AppSettings["apiKey"];
    var client = new SendGridClient(apiKey);

    string queryParams = @"{
    }";
    var response = await client.RequestAsync(method: SendGridClient.Method.GET, urlPath: "/suppression/bounces", queryParams: queryParams);
    Console.WriteLine(response.StatusCode);
    Console.WriteLine(response.Body.ReadAsStringAsync().Result);
    Console.WriteLine(response.Headers.ToString());
    Console.ReadLine();
}

3 个答案:

答案 0 :(得分:0)

SendGrid无法通过message_id查询。收据上提供了该信息,因此您可以将其与通过Event Webhook收到的事件相关联。

答案 1 :(得分:0)

3个选项:

  1. Subscribe to Webwook events,SendGrid将在其中发布消息的api返回事件。发送消息请求时,可以包含custom_args(apiV3)或unique_ids(apiV2)来唯一标识要发送的电子邮件。 Webhook响应中将包含您的唯一ID,因此您可以将原始电子邮件和由此产生的事件联系起来

  2. 如果您对Webhooks不太可靠,则可以付费购买附件以使用EmailActivity History Api。这将使您可以查询另一个Sendgrid API,以获取消息列表以及上次更新消息的时间or get a message by MessageId or by your uniqueId as per (1)。我相信您已有30天的历史记录,因此,如果您定期查询,可以在数据从服务器消失之前将响应存储在本地。

  3. 发送电子邮件并检查X-Message-ID值的响应标头。这是Sendgrid在将消息分别发送给每个收件人之前分配的值(然后,该收件人将生成“ MessageId”或sg_message_id)。然后,您可以使用query = select dt, nm, con_sum from ( select distinct dt, nm , sum(case when offload_seconds>=300 then 0 else offload_seconds end) over (partition by col_sum) as con_sum from ( select dt, nm, tm, offload_seconds , sum(case when offload_seconds>=300 then 1 else 0 end) over( rows between unbounded preceding and current row) as col_sum from tab2 ) a ) t where t.con_sum <>0; 例如,将EmailActivity History Api查询为GET / messages。 msg_id like "{X-Message-Id}%"

答案 2 :(得分:0)

这是一种方法,使用 LIKE 运算符

curl --request GET \
  --url 'https://api.sendgrid.com/v3/messages?limit=1&query=msg_id%20LIKE%20%22MSG_ID%25%22' \
  --header 'authorization: Bearer <<YOUR_SENDGRID_API_KEY_HERE>>' \
  --data '{}'