根据消息ID查询SendGrid API



因此,我正在搬到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("nnPress <Enter> to continue.");
    Console.ReadLine();
}

我想创建一个计划的任务,然后查询所有弹跳的电子邮件,并根据该电子邮件或实际任何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选项:

  1. 订阅Webwook事件,SendGrid将在其中发布您的API返回事件。发送消息请求时,您可以包含custom_args(apiv3(或unique_ids(apiv2(以唯一识别您发送的电子邮件。Webhook响应将具有您的唯一ID,因此您可以将原始电子邮件和结果事件绑定

  2. 如果Webhooks对您来说太不可靠了,则可以为使用电子邮件记录API付费。这将使您可以查询另一个sendgrid API以获取消息列表,并在上次更新时,或者根据(1(获得MessageID或唯一的消息。我相信您最多可以了解30天的历史,因此,如果您定期查询您可以在数据从服务器中消失之前在本地存储您的响应。

  3. 发送电子邮件并检查响应标头是否有X-Message-ID值。这是在单独发送给每个收件人(然后生成'MessageId'或sg_message_id(的值之前,将其分别发送给每个收件人。然后,您可以查询电子邮件activity历史记录API以获取/消息,并使用查询= msg_id like "{X-Message-Id}%",例如。msg_id like "abc-123-def%"

sendgrid无法通过message_id查询。收据上提供的内容,以便您可以将其与通过事件Webhook收到的事件相关联。

这是一种方法,使用类似的操作员

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 '{}'

最新更新