How can I cancel my scheduled mail in sendgrid?

1.5k views Asked by At

I have scheduling mail using 'send_at' with future time. Now I want to cancel that scheduled mail. How can achieve this problem. Please provide the solutions for it.

Thanks in advance.

3

There are 3 answers

0
jacobmovingfwd On

As per the documentation, you cannot cancel scheduled messages at this time: https://sendgrid.com/docs/API_Reference/SMTP_API/scheduling_parameters.html

0
Rastalamm On

Turns out that you can cancel scheduled email in sendgrid.

Here are the docs on how to do so

Mainly.. "Scheduled sends can be cancelled, if you include a batch ID with your send. "

The docs show you how to add the batch ID to your outbound messages so you can cancel them in the future.

0
PrashSE On

This can be doable , by creating and assigning batchid in request

  1. Create batch ID,

     var batchId="";
    
     var client = new SendGridClient(apikey);
    
      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;
    
  2. Added batchId in Message body

     var msg = new SendGridMessage()
     {
         From = new EmailAddress("[email protected]", ""),
         Subject = subject,
         PlainTextContent = message,
         HtmlContent = message,
         BatchId=batchId
     };
    
    
     var offset = new DateTimeOffset(timeToSend.Value);
     long sendAtUnixTime = offset.ToUnixTimeSeconds();
     msg.SendAt = sendAtUnixTime;
    
     msg.AddTo(new EmailAddress("[email protected]"));
     var response = await client.SendEmailAsync(msg);
    

3)Cancel a scheduled batch

    var batchId="yur batch id"
    var client = new SendGridClient(apiKey);
    string data = "{\"batch_id\":\"" + batchId +"\",\"status\": \"cancel\"}";
    var response = await client.RequestAsync(method: SendGridClient.Method.POST, 
    urlPath: "user/scheduled_sends", requestBody: data);

Note:

  • Make sure your api key has full access using sender grid option.

    Setting --> apiKeys ---> edit Api key--> select "full access" and update

  • When a batch is canceled, all messages associated with that batch will stay in your sending queue. When their send at value is reached, they will be discarded.

  • for more detail please refer enter link description here