我目前正在實施一系列 API 呼叫,這些呼叫將通過 SendGrid API 安排電子郵件。因此,這些電子郵件將進入分發串列,并且有可能需要根據業務用戶的需求重新安排。因此,我一直在嘗試將 BatchId 分配給 SendGridMessage,然后使用 BatchId 取消該訊息。
不幸的是,我繼續收到錯誤代碼,或者 API 的結果不太合理。例如,我成功地安排了電子郵件(我知道它是成功的,因為我在到達時間時收到了電子郵件。我也知道batchID 與訊息一起發送,因為我正在設定類別)。安排好后,我應該可以使用 batchId 并取消該批次。但是,我收到了無效的 batch_id 錯誤訊息。所以我運行 GET 命令來檢查 schedule_sends,并沒有回傳任何內容。
請查看下面的代碼,如果您有任何問題或疑慮,請告訴我。謝謝你。
與 SendGrid C# 包互動的 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"
}'
從上面的 curl 命令回傳結果
{"errors": [{
"field": "batch_id",
"message": "invalid batch id"
}]}
Edit: Overall workflow concept Generate email batchID --> Schedule email with batchID --> (Prolonged Wait) --> User wants to cancel batch --> Cancel email with previous batchID --> Gen new batchID and reschedule the existing email.
uj5u.com熱心網友回復:
關閉這個問題,因為我相信我沒有將電子郵件安排得足夠遠,以至于我實際上取消了它們。SendGrid 提到在 send_at 時間的 10 分鐘之前發送的取消請求不能保證被取消。我還注意到,當使用 user/scheduled_sends 端點時,在進行取消呼叫以及 batch_id 最終出現在串列中時會有延遲。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/452588.html
上一篇:我如何在.net6.0中使用dbInitializer.Initialize();
下一篇:如何撰寫介面的輸入和輸出泛型引數
