Sample code for 30+ languages & platforms
Unicode C++

Amazon SES v2: Publish delivery events to SNS and correlate a tagged send

See more Amazon SES Examples

Use an existing configuration set and Standard SNS topic in the same Region. Grant SES permission to publish to the topic and subscribe your consumer. Configure the destination once, then set configureDestination to false to send a test. In the SES event inside the SNS Message, eventType identifies the outcome, mail.messageId matches the send result, and mail.tags.notification_ref[0] identifies the application record. Use the SES bounce or complaint simulator address to exercise those outcomes.

Configure the SNS destination · SES event payload

Chilkat Unicode C++ Downloads

Unicode C++
#include <CkStringBuilderW.h>
#include <CkAuthAwsW.h>
#include <CkSocketW.h>
#include <CkRestW.h>
#include <CkJsonObjectW.h>

void ChilkatSample(void)
    {
    bool success = false;

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    success = false;

    bool configureDestination = true;
    const wchar_t *setName = L"application-mail";
    //  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);
    }

    if (configureDestination == true) {
        CkStringBuilderW path;
        path.Append(L"/v2/email/configuration-sets/");
        path.Append(setName);
        path.Append(L"/event-destinations");
        CkJsonObjectW destination;
        destination.UpdateString(L"EventDestinationName",L"delivery-to-sns");
        destination.UpdateString(L"EventDestination.MatchingEventTypes[0]",L"DELIVERY");
        destination.UpdateString(L"EventDestination.MatchingEventTypes[1]",L"BOUNCE");
        destination.UpdateString(L"EventDestination.MatchingEventTypes[2]",L"COMPLAINT");
        destination.UpdateString(L"EventDestination.SnsDestination.TopicArn",L"arn:aws:sns:us-west-2:123456789012:ses-delivery");
        destination.UpdateBool(L"EventDestination.Enabled",true);

        rest.AddHeader(L"Content-Type",L"application/json");
        const wchar_t *setupResponse = rest.fullRequestString(L"POST",path.getAsString(),destination.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(),setupResponse);
            return;
        }

        wprintf(L"Destination created. Use the send branch to test it.\n");
        return;
    }

    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.Simple.Subject.Data",L"Delivery event test");
    request.UpdateString(L"Content.Simple.Body.Text.Data",L"Exercise the application-mail configuration set.");
    request.UpdateString(L"EmailTags[0].Name",L"notification_ref");
    request.UpdateString(L"EmailTags[0].Value",L"notify-1042");
    request.UpdateString(L"ConfigurationSetName",setName);

    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"));
    }