Unicode C++
Unicode C++
Amazon SES v2: Send HTML with a plain text alternative
See more Amazon SES Examples
Send a single report notification with an HTML body and a plain text alternative. Each part conveys the same information so the recipient can read it with either kind of mail client. Use a verified sender in the sending Region and grant ses:SendEmail.
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;
// Replace the From address with your verified sender. The destination is the SES mailbox simulator.
CkJsonObjectW request;
request.UpdateString(L"FromEmailAddress",L"sender+reports@example.com");
request.UpdateString(L"Destination.ToAddresses[0]",L"success@simulator.amazonses.com");
request.UpdateString(L"Content.Simple.Subject.Data",L"Weekly report: 12 orders shipped");
request.UpdateString(L"Content.Simple.Subject.Charset",L"UTF-8");
// SES builds a multipart/alternative message from the two body representations.
request.UpdateString(L"Content.Simple.Body.Text.Charset",L"UTF-8");
request.UpdateString(L"Content.Simple.Body.Text.Data",L"Weekly report: 12 orders shipped.");
request.UpdateString(L"Content.Simple.Body.Html.Charset",L"UTF-8");
request.UpdateString(L"Content.Simple.Body.Html.Data",L"<h1>Weekly report</h1><p><strong>12 orders</strong> shipped.</p>");
// 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 result;
success = result.Load(responseText);
if (success != true) {
wprintf(L"%s\n",result.lastErrorText());
return;
}
if (result.HasMember(L"MessageId") != true) {
wprintf(L"SES returned no message ID.\n");
return;
}
wprintf(L"SES accepted the message. Message ID: %s\n",result.stringOf(L"MessageId"));
}