Sample code for 30+ languages & platforms
Unicode C

Amazon SES v2: Send a newsletter with an SES-managed topic unsubscribe link

See more Amazon SES Examples

Use an existing SES contact list named Readers with a ProductUpdates topic and a test subscriber who opted in. Replace the verified sender and subscriber address. Send to one recipient per message so SES can insert its subscription headers and personalize the footer link. When that reader opts out, SES honors the preference on future sends using the same list and topic.

SES subscription management · SendEmail

Chilkat Unicode C Downloads

Unicode C
#include <C_CkJsonObjectW.h>
#include <C_CkStringBuilderW.h>
#include <C_CkAuthAwsW.h>
#include <C_CkSocketW.h>
#include <C_CkRestW.h>

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

    success = FALSE;

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

    request = CkJsonObjectW_Create();
    CkJsonObjectW_UpdateString(request,L"FromEmailAddress",L"sender@example.com");
    CkJsonObjectW_UpdateString(request,L"Destination.ToAddresses[0]",L"subscriber@example.com");
    CkJsonObjectW_UpdateString(request,L"ListManagementOptions.ContactListName",L"Readers");
    CkJsonObjectW_UpdateString(request,L"ListManagementOptions.TopicName",L"ProductUpdates");
    CkJsonObjectW_UpdateString(request,L"Content.Simple.Subject.Data",L"Product updates: November edition");
    CkJsonObjectW_UpdateString(request,L"Content.Simple.Body.Text.Data",L"The November catalog is available.\nManage your preferences: {{amazonSESUnsubscribeUrl}}");
    CkJsonObjectW_UpdateString(request,L"Content.Simple.Body.Html.Data",L"<p>The November catalog is available.</p><p><a href=\"{{amazonSESUnsubscribeUrl}}\">Manage your product update preferences</a></p>");
    //  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));
        CkJsonObjectW_Dispose(request);
        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));
        CkJsonObjectW_Dispose(request);
        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));
        CkJsonObjectW_Dispose(request);
        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);
    }

    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));
        CkJsonObjectW_Dispose(request);
        CkStringBuilderW_Dispose(host);
        CkAuthAwsW_Dispose(signer);
        CkSocketW_Dispose(connection);
        CkRestW_Dispose(rest);
        return;
    }

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

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

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


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

    }