Unicode C++
Unicode C++
Amazon SES v2: Send a personalized receipt from a stored template
See more Amazon SES Examples
First create and preview the OrderReceipt template in the sending Region. Supply a verified sender. Build the order as structured data, then serialize it once into the string required by TemplateData. A message ID records acceptance. Rendering failures can be reported through SES event publishing.
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 order;
order.UpdateString(L"firstName",L"Avery");
order.UpdateString(L"orderNumber",L"1042");
order.UpdateString(L"items[0].name",L"Notebook");
order.UpdateInt(L"items[0].quantity",2);
order.UpdateString(L"items[1].name",L"Pen");
order.UpdateInt(L"items[1].quantity",1);
CkJsonObjectW request;
request.UpdateString(L"FromEmailAddress",L"sender@example.com");
request.UpdateString(L"Destination.ToAddresses[0]",L"success@simulator.amazonses.com");
request.UpdateString(L"Content.Template.TemplateName",L"OrderReceipt");
request.UpdateString(L"Content.Template.TemplateData",order.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-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;
}
wprintf(L"SES accepted the message: %s\n",response.stringOf(L"MessageId"));
}