C# Requires Chilkat v11.0.0+
C#
SendGrid: Schedule a reminder and cancel it by batch ID
See more SendGrid Examples
Schedule a reminder one hour from now. Save its batch ID when it is allocated. If the appointment is canceled, use that saved ID in the cancellation branch. Mail Send accepts a Unix timestamp up to 72 hours ahead; cancel at least 10 minutes before the send time. Cancellation applies to the entire batch.
Create a batch ID ยท Cancel a batch
Chilkat C# Downloads
bool success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
success = false;
Chilkat.Http http = new Chilkat.Http();
http.RequireSslCertVerify = true;
// Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
http.AuthToken = "SENDGRID_API_KEY";
Chilkat.HttpResponse resp = new Chilkat.HttpResponse();
// On cancellation, set this to the batch ID saved with the original appointment.
// This branch does not create a new batch or send another reminder.
string batchToCancel = "";
if (batchToCancel != "") {
Chilkat.JsonObject cancellation = new Chilkat.JsonObject();
cancellation.UpdateString("batch_id",batchToCancel);
cancellation.UpdateString("status","cancel");
success = http.HttpJson("POST","https://api.sendgrid.com/v3/user/scheduled_sends",cancellation,"application/json",resp);
if (success != true) {
if (resp.StatusCode == 0) {
Debug.WriteLine(http.LastErrorText);
}
else {
Debug.WriteLine("HTTP " + Convert.ToString(resp.StatusCode) + ": " + resp.BodyStr);
}
return;
}
if (resp.StatusCode != 201) {
Debug.WriteLine("HTTP " + Convert.ToString(resp.StatusCode) + ": " + resp.BodyStr);
return;
}
Debug.WriteLine("Batch cancellation accepted.");
return;
}
success = http.HttpNoBody("POST","https://api.sendgrid.com/v3/mail/batch",resp);
if (success != true) {
if (resp.StatusCode == 0) {
Debug.WriteLine(http.LastErrorText);
}
else {
Debug.WriteLine("HTTP " + Convert.ToString(resp.StatusCode) + ": " + resp.BodyStr);
}
return;
}
if (resp.StatusCode != 201) {
Debug.WriteLine("HTTP " + Convert.ToString(resp.StatusCode) + ": " + resp.BodyStr);
return;
}
Chilkat.JsonObject batch = new Chilkat.JsonObject();
success = batch.Load(resp.BodyStr);
if (success != true) {
Debug.WriteLine("Unexpected response: expected JSON.");
return;
}
string batchId = batch.StringOf("batch_id");
if (batch.HasMember("batch_id") != true) {
Debug.WriteLine("Missing batch_id in response.");
return;
}
// Allocate one batch per independently cancelable appointment.
Debug.WriteLine("Save this batch ID with the appointment: " + batchId);
Chilkat.CkDateTime sendTime = new Chilkat.CkDateTime();
sendTime.SetFromCurrentSystemTime();
sendTime.AddSeconds(3600);
Chilkat.JsonObject message = new Chilkat.JsonObject();
message.UpdateString("batch_id",batchId);
message.UpdateInt("send_at",sendTime.GetAsUnixTime(false));
message.UpdateString("from.email","sender@example.com");
message.UpdateString("personalizations[0].to[0].email","recipient@example.com");
message.UpdateString("subject","Appointment reminder");
message.UpdateString("content[0].type","text/plain");
message.UpdateString("content[0].value","Your appointment is tomorrow at 9:30 AM Central.");
success = http.HttpJson("POST","https://api.sendgrid.com/v3/mail/send",message,"application/json",resp);
if (success != true) {
if (resp.StatusCode == 0) {
Debug.WriteLine(http.LastErrorText);
}
else {
Debug.WriteLine("HTTP " + Convert.ToString(resp.StatusCode) + ": " + resp.BodyStr);
}
return;
}
if (resp.StatusCode != 202) {
Debug.WriteLine("HTTP " + Convert.ToString(resp.StatusCode) + ": " + resp.BodyStr);
return;
}
Debug.WriteLine("Reminder scheduled for " + sendTime.GetAsTimestamp(false));