使用 C# 在 SendGrid 中取消 BatchId API
Cancelling BatchId in SendGrid Using C# API
我目前正在实施一系列 API 调用,这些调用将通过 SendGrid API 安排电子邮件。因此,这些电子邮件将进入分发列表,并有可能需要根据业务用户的需求重新安排。因此,我一直在尝试将 BatchId 分配给 SendGridMessage,然后使用 BatchId 取消该消息。
不幸的是,我继续收到错误代码,或者 API 的结果不太合理。例如,我成功地安排了电子邮件(我知道这是成功的,因为我在到达时间时收到了电子邮件。我也知道 batchID 是随消息一起发送的,因为我正在设置类别)。安排好后,我应该可以获取 batchId 并取消该批次。但是,我收到无效的 batch_id 错误消息。所以我 运行 GET 命令检查 scheduled_sends,没有任何返回。
请查看下面的代码,如果您有任何问题或疑虑,请告诉我。谢谢。
C# 代码与 SendGrid C# 包交互
message.BatchId = sendGridBatchID;
if (sendAt != 0)
{
message.SendAt = sendAt;
}
var apiKey = _configuration.GetSection("NewSendGridAPIKey").Value;
var client = new SendGridClient(apiKey);
var response = await client.SendEmailAsync(message).ConfigureAwait(false);
return response.IsSuccessStatusCode;
用于生成新批次 ID 的 C# 代码
string apiKey = GetAPIKey();
SendGridClient client = GetSendGridClient(apiKey);
var batchId = default(string);
var response = await client.RequestAsync(method: SendGridClient.Method.POST, urlPath: "mail/batch");
if (response.StatusCode == HttpStatusCode.Created)
{
JObject joResponse = JObject.Parse(response.Body.ReadAsStringAsync().Result);
batchId = (((Newtonsoft.Json.Linq.JValue)joResponse["batch_id"]).Value).ToString();
}
return batchId;
curl命令查看是否可以取消预定的批处理
curl --location --request POST 'https://api.sendgrid.com/v3/user/scheduled_sends' \
--header 'Authorization: Bearer <<API_KEY>>' \
--header 'Content-Type: application/json' \
--data-raw '{
"batch_id": "<BATCH_ID>",
"status":"cancel"
}'
Return 上述 curl 命令的结果
{"errors": [{
"field": "batch_id",
"message": "invalid batch id"
}]}
编辑:整体工作流程概念
生成电子邮件 batchID --> 使用 batchID 安排电子邮件 --> (长时间等待) --> 用户想要取消批次 --> 使用以前的 batchID 取消电子邮件 --> 生成新的 batchID 并重新安排现有电子邮件。
关闭这个问题,因为我认为我没有将电子邮件安排得足够远,以至于我无法真正取消它们。 SendGrid 提到在 send_at 时间 10 分钟之前发送的取消请求不保证会被取消。我还注意到,在使用 user/scheduled_sends 端点时,取消调用以及 batch_id 出现在列表中时会有延迟。
我目前正在实施一系列 API 调用,这些调用将通过 SendGrid API 安排电子邮件。因此,这些电子邮件将进入分发列表,并有可能需要根据业务用户的需求重新安排。因此,我一直在尝试将 BatchId 分配给 SendGridMessage,然后使用 BatchId 取消该消息。
不幸的是,我继续收到错误代码,或者 API 的结果不太合理。例如,我成功地安排了电子邮件(我知道这是成功的,因为我在到达时间时收到了电子邮件。我也知道 batchID 是随消息一起发送的,因为我正在设置类别)。安排好后,我应该可以获取 batchId 并取消该批次。但是,我收到无效的 batch_id 错误消息。所以我 运行 GET 命令检查 scheduled_sends,没有任何返回。
请查看下面的代码,如果您有任何问题或疑虑,请告诉我。谢谢。
C# 代码与 SendGrid C# 包交互
message.BatchId = sendGridBatchID;
if (sendAt != 0)
{
message.SendAt = sendAt;
}
var apiKey = _configuration.GetSection("NewSendGridAPIKey").Value;
var client = new SendGridClient(apiKey);
var response = await client.SendEmailAsync(message).ConfigureAwait(false);
return response.IsSuccessStatusCode;
用于生成新批次 ID 的 C# 代码
string apiKey = GetAPIKey();
SendGridClient client = GetSendGridClient(apiKey);
var batchId = default(string);
var response = await client.RequestAsync(method: SendGridClient.Method.POST, urlPath: "mail/batch");
if (response.StatusCode == HttpStatusCode.Created)
{
JObject joResponse = JObject.Parse(response.Body.ReadAsStringAsync().Result);
batchId = (((Newtonsoft.Json.Linq.JValue)joResponse["batch_id"]).Value).ToString();
}
return batchId;
curl命令查看是否可以取消预定的批处理
curl --location --request POST 'https://api.sendgrid.com/v3/user/scheduled_sends' \
--header 'Authorization: Bearer <<API_KEY>>' \
--header 'Content-Type: application/json' \
--data-raw '{
"batch_id": "<BATCH_ID>",
"status":"cancel"
}'
Return 上述 curl 命令的结果
{"errors": [{
"field": "batch_id",
"message": "invalid batch id"
}]}
编辑:整体工作流程概念 生成电子邮件 batchID --> 使用 batchID 安排电子邮件 --> (长时间等待) --> 用户想要取消批次 --> 使用以前的 batchID 取消电子邮件 --> 生成新的 batchID 并重新安排现有电子邮件。
关闭这个问题,因为我认为我没有将电子邮件安排得足够远,以至于我无法真正取消它们。 SendGrid 提到在 send_at 时间 10 分钟之前发送的取消请求不保证会被取消。我还注意到,在使用 user/scheduled_sends 端点时,取消调用以及 batch_id 出现在列表中时会有延迟。