Unicode C++ Requires Chilkat v11.0.0+
Unicode 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 Unicode C++ Downloads
#include <CkHttpW.h>
#include <CkHttpResponseW.h>
#include <CkJsonObjectW.h>
#include <CkDateTimeW.h>
void ChilkatSample(void)
{
bool success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
success = false;
CkHttpW http;
http.put_RequireSslCertVerify(true);
// Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
http.put_AuthToken(L"SENDGRID_API_KEY");
CkHttpResponseW resp;
// 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.
const wchar_t *batchToCancel = L"";
if (batchToCancel != L"") {
CkJsonObjectW cancellation;
cancellation.UpdateString(L"batch_id",batchToCancel);
cancellation.UpdateString(L"status",L"cancel");
success = http.HttpJson(L"POST",L"https://api.sendgrid.com/v3/user/scheduled_sends",cancellation,L"application/json",resp);
if (success != true) {
if (resp.get_StatusCode() == 0) {
wprintf(L"%s\n",http.lastErrorText());
}
else {
wprintf(L"HTTP %d: %s\n",resp.get_StatusCode(),resp.bodyStr());
}
return;
}
if (resp.get_StatusCode() != 201) {
wprintf(L"HTTP %d: %s\n",resp.get_StatusCode(),resp.bodyStr());
return;
}
wprintf(L"Batch cancellation accepted.\n");
return;
}
success = http.HttpNoBody(L"POST",L"https://api.sendgrid.com/v3/mail/batch",resp);
if (success != true) {
if (resp.get_StatusCode() == 0) {
wprintf(L"%s\n",http.lastErrorText());
}
else {
wprintf(L"HTTP %d: %s\n",resp.get_StatusCode(),resp.bodyStr());
}
return;
}
if (resp.get_StatusCode() != 201) {
wprintf(L"HTTP %d: %s\n",resp.get_StatusCode(),resp.bodyStr());
return;
}
CkJsonObjectW batch;
success = batch.Load(resp.bodyStr());
if (success != true) {
wprintf(L"Unexpected response: expected JSON.\n");
return;
}
const wchar_t *batchId = batch.stringOf(L"batch_id");
if (batch.HasMember(L"batch_id") != true) {
wprintf(L"Missing batch_id in response.\n");
return;
}
// Allocate one batch per independently cancelable appointment.
wprintf(L"Save this batch ID with the appointment: %s\n",batchId);
CkDateTimeW sendTime;
sendTime.SetFromCurrentSystemTime();
sendTime.AddSeconds(3600);
CkJsonObjectW message;
message.UpdateString(L"batch_id",batchId);
message.UpdateInt(L"send_at",sendTime.GetAsUnixTime(false));
message.UpdateString(L"from.email",L"sender@example.com");
message.UpdateString(L"personalizations[0].to[0].email",L"recipient@example.com");
message.UpdateString(L"subject",L"Appointment reminder");
message.UpdateString(L"content[0].type",L"text/plain");
message.UpdateString(L"content[0].value",L"Your appointment is tomorrow at 9:30 AM Central.");
success = http.HttpJson(L"POST",L"https://api.sendgrid.com/v3/mail/send",message,L"application/json",resp);
if (success != true) {
if (resp.get_StatusCode() == 0) {
wprintf(L"%s\n",http.lastErrorText());
}
else {
wprintf(L"HTTP %d: %s\n",resp.get_StatusCode(),resp.bodyStr());
}
return;
}
if (resp.get_StatusCode() != 202) {
wprintf(L"HTTP %d: %s\n",resp.get_StatusCode(),resp.bodyStr());
return;
}
wprintf(L"Reminder scheduled for %s\n",sendTime.getAsTimestamp(false));
}