Unicode C++
Unicode C++
Amazon SES v2: Send private appointment reminders and inspect each result
See more Amazon SES Examples
Replace the two recipients with test mailboxes you control. The example supplies an inline template and one private destination per entry. Store each successful message ID against its appointment. Schedule a retry with backoff only for entries with a retryable outcome. Correct permanent errors before resubmission.
SendBulkEmail · Per-entry outcomes
Chilkat Unicode C++ Downloads
#include <CkJsonObjectW.h>
#include <CkStringBuilderW.h>
#include <CkAuthAwsW.h>
#include <CkSocketW.h>
#include <CkRestW.h>
void ChilkatSample(void)
{
bool success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
success = false;
CkJsonObjectW request;
request.UpdateString(L"FromEmailAddress",L"sender@example.com");
request.UpdateString(L"DefaultContent.Template.TemplateContent.Subject",L"Appointment reminder");
request.UpdateString(L"DefaultContent.Template.TemplateContent.Text",L"Hello {{name}}, your appointment is {{slot}}.");
request.UpdateString(L"DefaultContent.Template.TemplateData",L"{\"name\":\"there\",\"slot\":\"in your booking confirmation\"}");
CkJsonObjectW firstReminder;
firstReminder.UpdateString(L"name",L"Avery");
firstReminder.UpdateString(L"slot",L"Tuesday at 9 AM Central");
request.UpdateString(L"BulkEmailEntries[0].Destination.ToAddresses[0]",L"avery@example.com");
request.UpdateString(L"BulkEmailEntries[0].ReplacementEmailContent.ReplacementTemplate.ReplacementTemplateData",firstReminder.emit());
CkJsonObjectW secondReminder;
secondReminder.UpdateString(L"name",L"Robin");
secondReminder.UpdateString(L"slot",L"Wednesday at 2 PM Central");
request.UpdateString(L"BulkEmailEntries[1].Destination.ToAddresses[0]",L"robin@example.com");
request.UpdateString(L"BulkEmailEntries[1].ReplacementEmailContent.ReplacementTemplate.ReplacementTemplateData",secondReminder.emit());
// Use the same AWS Region for the identity, endpoint, and Signature Version 4.
const wchar_t *region = L"us-west-2";
CkStringBuilderW host;
host.Append(L"email.");
host.Append(region);
host.Append(L".amazonaws.com");
CkAuthAwsW signer;
// Supply the credentials from your application's credential provider.
signer.put_AccessKey(L"AWS_ACCESS_KEY_ID");
signer.put_SecretKey(L"AWS_SECRET_ACCESS_KEY");
signer.put_Region(region);
signer.put_ServiceName(L"ses");
CkSocketW connection;
connection.put_RequireSslCertVerify(true);
success = connection.Connect(host.getAsString(),443,true,10000);
if (success != true) {
wprintf(L"%s\n",connection.lastErrorText());
return;
}
CkRestW rest;
success = rest.UseConnection(connection,false);
if (success != true) {
wprintf(L"%s\n",rest.lastErrorText());
return;
}
success = rest.SetAuthAws(signer);
if (success != true) {
wprintf(L"%s\n",rest.lastErrorText());
return;
}
// For temporary AWS credentials, supply the session token from the same provider.
const wchar_t *sessionToken = L"";
if (sessionToken != L"") {
rest.AddQueryParam(L"X-Amz-Security-Token",sessionToken);
}
rest.AddHeader(L"Content-Type",L"application/json");
const wchar_t *responseText = rest.fullRequestString(L"POST",L"/v2/email/outbound-bulk-emails",request.emit());
if (rest.get_LastMethodSuccess() != true) {
wprintf(L"%s\n",rest.lastErrorText());
return;
}
if (rest.get_ResponseStatusCode() != 200) {
wprintf(L"SES returned HTTP %d %s\n",rest.get_ResponseStatusCode(),responseText);
return;
}
CkJsonObjectW response;
success = response.Load(responseText);
if (success != true) {
wprintf(L"%s\n",response.lastErrorText());
return;
}
int count = response.SizeOfArray(L"BulkEmailEntryResults");
if (count != request.SizeOfArray(L"BulkEmailEntries")) {
wprintf(L"Incomplete results. Reconcile the original batch before retrying.\n");
return;
}
int i = 0;
while ((i < count)) {
response.put_I(i);
request.put_I(i);
const wchar_t *recipient = request.stringOf(L"BulkEmailEntries[i].Destination.ToAddresses[0]");
const wchar_t *status = response.stringOf(L"BulkEmailEntryResults[i].Status");
if (status == L"SUCCESS") {
wprintf(L"%s accepted: %s\n",recipient,response.stringOf(L"BulkEmailEntryResults[i].MessageId"));
}
else {
if ((status == L"ACCOUNT_THROTTLED") || (status == L"TRANSIENT_FAILURE")) {
wprintf(L"%s retry later: %s\n",recipient,status);
}
else {
wprintf(L"%s review before retry: %s %s\n",recipient,status,response.stringOf(L"BulkEmailEntryResults[i].Error"));
}
}
i = i + 1;
}
}