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 <C_CkStringBuilderW.h>
#include <C_CkAuthAwsW.h>
#include <C_CkSocketW.h>
#include <C_CkRestW.h>
#include <C_CkJsonObjectW.h>

void ChilkatSample(void)
    {
    BOOL success;
    BOOL configureDestination;
    const wchar_t *setName;
    const wchar_t *region;
    HCkStringBuilderW host;
    HCkAuthAwsW signer;
    HCkSocketW connection;
    HCkRestW rest;
    const wchar_t *sessionToken;
    HCkStringBuilderW path;
    HCkJsonObjectW destination;
    const wchar_t *setupResponse;
    HCkJsonObjectW request;
    const wchar_t *responseText;
    HCkJsonObjectW response;

    success = FALSE;

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

    configureDestination = TRUE;
    setName = L"application-mail";
    //  Use the same AWS Region for the identity, endpoint, and Signature Version 4.
    region = L"us-west-2";
    host = CkStringBuilderW_Create();
    CkStringBuilderW_Append(host,L"email.");
    CkStringBuilderW_Append(host,region);
    CkStringBuilderW_Append(host,L".amazonaws.com");

    signer = CkAuthAwsW_Create();
    //  Supply the credentials from your application's credential provider.
    CkAuthAwsW_putAccessKey(signer,L"AWS_ACCESS_KEY_ID");
    CkAuthAwsW_putSecretKey(signer,L"AWS_SECRET_ACCESS_KEY");
    CkAuthAwsW_putRegion(signer,region);
    CkAuthAwsW_putServiceName(signer,L"ses");

    connection = CkSocketW_Create();
    CkSocketW_putRequireSslCertVerify(connection,TRUE);
    success = CkSocketW_Connect(connection,CkStringBuilderW_getAsString(host),443,TRUE,10000);
    if (success != TRUE) {
        wprintf(L"%s\n",CkSocketW_lastErrorText(connection));
        CkStringBuilderW_Dispose(host);
        CkAuthAwsW_Dispose(signer);
        CkSocketW_Dispose(connection);
        return;
    }

    rest = CkRestW_Create();
    success = CkRestW_UseConnection(rest,connection,FALSE);
    if (success != TRUE) {
        wprintf(L"%s\n",CkRestW_lastErrorText(rest));
        CkStringBuilderW_Dispose(host);
        CkAuthAwsW_Dispose(signer);
        CkSocketW_Dispose(connection);
        CkRestW_Dispose(rest);
        return;
    }

    success = CkRestW_SetAuthAws(rest,signer);
    if (success != TRUE) {
        wprintf(L"%s\n",CkRestW_lastErrorText(rest));
        CkStringBuilderW_Dispose(host);
        CkAuthAwsW_Dispose(signer);
        CkSocketW_Dispose(connection);
        CkRestW_Dispose(rest);
        return;
    }

    //  For temporary AWS credentials, supply the session token from the same provider.
    sessionToken = L"";
    if (sessionToken != L"") {
        CkRestW_AddQueryParam(rest,L"X-Amz-Security-Token",sessionToken);
    }

    if (configureDestination == TRUE) {
        path = CkStringBuilderW_Create();
        CkStringBuilderW_Append(path,L"/v2/email/configuration-sets/");
        CkStringBuilderW_Append(path,setName);
        CkStringBuilderW_Append(path,L"/event-destinations");
        destination = CkJsonObjectW_Create();
        CkJsonObjectW_UpdateString(destination,L"EventDestinationName",L"delivery-to-sns");
        CkJsonObjectW_UpdateString(destination,L"EventDestination.MatchingEventTypes[0]",L"DELIVERY");
        CkJsonObjectW_UpdateString(destination,L"EventDestination.MatchingEventTypes[1]",L"BOUNCE");
        CkJsonObjectW_UpdateString(destination,L"EventDestination.MatchingEventTypes[2]",L"COMPLAINT");
        CkJsonObjectW_UpdateString(destination,L"EventDestination.SnsDestination.TopicArn",L"arn:aws:sns:us-west-2:123456789012:ses-delivery");
        CkJsonObjectW_UpdateBool(destination,L"EventDestination.Enabled",TRUE);

        CkRestW_AddHeader(rest,L"Content-Type",L"application/json");
        setupResponse = CkRestW_fullRequestString(rest,L"POST",CkStringBuilderW_getAsString(path),CkJsonObjectW_emit(destination));
        if (CkRestW_getLastMethodSuccess(rest) != TRUE) {
            wprintf(L"%s\n",CkRestW_lastErrorText(rest));
            CkStringBuilderW_Dispose(host);
            CkAuthAwsW_Dispose(signer);
            CkSocketW_Dispose(connection);
            CkRestW_Dispose(rest);
            CkStringBuilderW_Dispose(path);
            CkJsonObjectW_Dispose(destination);
            return;
        }

        if (CkRestW_getResponseStatusCode(rest) != 200) {
            wprintf(L"SES returned HTTP %d %s\n",CkRestW_getResponseStatusCode(rest),setupResponse);
            CkStringBuilderW_Dispose(host);
            CkAuthAwsW_Dispose(signer);
            CkSocketW_Dispose(connection);
            CkRestW_Dispose(rest);
            CkStringBuilderW_Dispose(path);
            CkJsonObjectW_Dispose(destination);
            return;
        }

        wprintf(L"Destination created. Use the send branch to test it.\n");
        CkStringBuilderW_Dispose(host);
        CkAuthAwsW_Dispose(signer);
        CkSocketW_Dispose(connection);
        CkRestW_Dispose(rest);
        CkStringBuilderW_Dispose(path);
        CkJsonObjectW_Dispose(destination);
        return;
    }

    request = CkJsonObjectW_Create();
    CkJsonObjectW_UpdateString(request,L"FromEmailAddress",L"sender@example.com");
    CkJsonObjectW_UpdateString(request,L"Destination.ToAddresses[0]",L"success@simulator.amazonses.com");
    CkJsonObjectW_UpdateString(request,L"Content.Simple.Subject.Data",L"Delivery event test");
    CkJsonObjectW_UpdateString(request,L"Content.Simple.Body.Text.Data",L"Exercise the application-mail configuration set.");
    CkJsonObjectW_UpdateString(request,L"EmailTags[0].Name",L"notification_ref");
    CkJsonObjectW_UpdateString(request,L"EmailTags[0].Value",L"notify-1042");
    CkJsonObjectW_UpdateString(request,L"ConfigurationSetName",setName);

    CkRestW_AddHeader(rest,L"Content-Type",L"application/json");
    responseText = CkRestW_fullRequestString(rest,L"POST",L"/v2/email/outbound-emails",CkJsonObjectW_emit(request));
    if (CkRestW_getLastMethodSuccess(rest) != TRUE) {
        wprintf(L"%s\n",CkRestW_lastErrorText(rest));
        CkStringBuilderW_Dispose(host);
        CkAuthAwsW_Dispose(signer);
        CkSocketW_Dispose(connection);
        CkRestW_Dispose(rest);
        CkStringBuilderW_Dispose(path);
        CkJsonObjectW_Dispose(destination);
        CkJsonObjectW_Dispose(request);
        return;
    }

    if (CkRestW_getResponseStatusCode(rest) != 200) {
        wprintf(L"SES returned HTTP %d %s\n",CkRestW_getResponseStatusCode(rest),responseText);
        CkStringBuilderW_Dispose(host);
        CkAuthAwsW_Dispose(signer);
        CkSocketW_Dispose(connection);
        CkRestW_Dispose(rest);
        CkStringBuilderW_Dispose(path);
        CkJsonObjectW_Dispose(destination);
        CkJsonObjectW_Dispose(request);
        return;
    }

    response = CkJsonObjectW_Create();
    success = CkJsonObjectW_Load(response,responseText);
    if (success != TRUE) {
        wprintf(L"%s\n",CkJsonObjectW_lastErrorText(response));
        CkStringBuilderW_Dispose(host);
        CkAuthAwsW_Dispose(signer);
        CkSocketW_Dispose(connection);
        CkRestW_Dispose(rest);
        CkStringBuilderW_Dispose(path);
        CkJsonObjectW_Dispose(destination);
        CkJsonObjectW_Dispose(request);
        CkJsonObjectW_Dispose(response);
        return;
    }

    wprintf(L"SES accepted the message: %s\n",CkJsonObjectW_stringOf(response,L"MessageId"));


    CkStringBuilderW_Dispose(host);
    CkAuthAwsW_Dispose(signer);
    CkSocketW_Dispose(connection);
    CkRestW_Dispose(rest);
    CkStringBuilderW_Dispose(path);
    CkJsonObjectW_Dispose(destination);
    CkJsonObjectW_Dispose(request);
    CkJsonObjectW_Dispose(response);

    }