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

Amazon SES v2: Check an address on the account suppression list

See more Amazon SES Examples

Look up one address in the sending Region with ses:GetSuppressedDestination. The record distinguishes a bounce from a complaint. An absent record tells you only that this account suppression lookup found no entry. The example signs the exact encoded mailbox path and also works with temporary credentials.

GetSuppressedDestination

Chilkat Unicode C++ Downloads

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

void ChilkatSample(void)
    {
    bool success = false;

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

    //  Look up the address exactly as stored, including any plus tag.
    const wchar_t *address = L"avery+orders@example.com";
    const wchar_t *region = L"us-west-2";
    const wchar_t *accessKey = L"AWS_ACCESS_KEY_ID";
    const wchar_t *secretKey = L"AWS_SECRET_ACCESS_KEY";
    const wchar_t *sessionToken = L"";
    CkStringBuilderW host;
    host.Append(L"email.");
    host.Append(region);
    host.Append(L".amazonaws.com");
    CkStringBuilderW mailbox;
    mailbox.Append(address);
    CkStringBuilderW path;
    path.Append(L"/v2/email/suppression/addresses/");
    path.Append(mailbox.getEncoded(L"url_rfc3986",L"utf-8"));

    //  SigV4 for SES encodes the wire path a second time in the canonical request.
    //  Only the canonical copy escapes the percent signs. Send the original path over HTTPS.
    CkStringBuilderW canonicalPath;
    canonicalPath.Append(path.getAsString());
    int replaced = canonicalPath.Replace(L"%",L"%25");
    CkDateTimeW clock;
    clock.SetFromCurrentSystemTime();
    CkStringBuilderW stamp;
    stamp.Append(clock.getAsTimestamp(false));
    replaced = stamp.Replace(L"-",L"");
    replaced = stamp.Replace(L":",L"");
    const wchar_t *date = stamp.getRange(0,8,false);
    CkRestW rest;
    rest.AddHeader(L"Host",host.getAsString());
    rest.AddHeader(L"x-amz-date",stamp.getAsString());
    CkStringBuilderW headers;
    headers.Append(L"host:");
    headers.Append(host.getAsString());
    headers.Append(L"\nx-amz-date:");
    headers.Append(stamp.getAsString());
    headers.Append(L"\n");
    const wchar_t *signedHeaders = L"host;x-amz-date";
    if (sessionToken != L"") {
        rest.AddHeader(L"x-amz-security-token",sessionToken);
        headers.Append(L"x-amz-security-token:");
        headers.Append(sessionToken);
        headers.Append(L"\n");
        signedHeaders = L"host;x-amz-date;x-amz-security-token";
    }

    CkCrypt2W crypto;
    crypto.put_HashAlgorithm(L"sha256");
    crypto.put_MacAlgorithm(L"hmac");
    crypto.put_EncodingMode(L"hexlower");
    crypto.put_Charset(L"utf-8");
    CkStringBuilderW canonical;
    canonical.Append(L"GET\n");
    canonical.Append(canonicalPath.getAsString());
    canonical.Append(L"\n\n");
    canonical.Append(headers.getAsString());
    canonical.Append(L"\n");
    canonical.Append(signedHeaders);
    canonical.Append(L"\n");
    canonical.Append(crypto.hashStringENC(L""));
    CkStringBuilderW scope;
    scope.Append(date);
    scope.Append(L"/");
    scope.Append(region);
    scope.Append(L"/ses/aws4_request");
    CkStringBuilderW toSign;
    toSign.Append(L"AWS4-HMAC-SHA256\n");
    toSign.Append(stamp.getAsString());
    toSign.Append(L"\n");
    toSign.Append(scope.getAsString());
    toSign.Append(L"\n");
    toSign.Append(crypto.hashStringENC(canonical.getAsString()));

    //  Derive the signing key one HMAC at a time. Decode each intermediate key from hex.
    CkStringBuilderW seed;
    seed.Append(L"AWS4");
    seed.Append(secretKey);
    crypto.SetMacKeyString(seed.getAsString());
    const wchar_t *key = crypto.macStringENC(date);
    crypto.SetMacKeyEncoded(key,L"hex");
    key = crypto.macStringENC(region);
    crypto.SetMacKeyEncoded(key,L"hex");
    key = crypto.macStringENC(L"ses");
    crypto.SetMacKeyEncoded(key,L"hex");
    key = crypto.macStringENC(L"aws4_request");
    crypto.SetMacKeyEncoded(key,L"hex");
    const wchar_t *signature = crypto.macStringENC(toSign.getAsString());
    CkStringBuilderW authorization;
    authorization.Append(L"AWS4-HMAC-SHA256 Credential=");
    authorization.Append(accessKey);
    authorization.Append(L"/");
    authorization.Append(scope.getAsString());
    authorization.Append(L", SignedHeaders=");
    authorization.Append(signedHeaders);
    authorization.Append(L", Signature=");
    authorization.Append(signature);
    rest.AddHeader(L"Authorization",authorization.getAsString());
    CkSocketW connection;
    connection.put_RequireSslCertVerify(true);
    success = connection.Connect(host.getAsString(),443,true,10000);
    if (success != true) {
        wprintf(L"%s\n",connection.lastErrorText());
        return;
    }

    success = rest.UseConnection(connection,false);
    if (success != true) {
        wprintf(L"%s\n",rest.lastErrorText());
        return;
    }

    const wchar_t *responseText = rest.fullRequestNoBody(L"GET",path.getAsString());
    if (rest.get_LastMethodSuccess() != true) {
        wprintf(L"%s\n",rest.lastErrorText());
        return;
    }

    if (rest.get_ResponseStatusCode() == 404) {
        wprintf(L"No account suppression entry for this address in this Region.\n");
        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"%s\n",response.stringOf(L"SuppressedDestination.EmailAddress"));
    wprintf(L"Reason: %s\n",response.stringOf(L"SuppressedDestination.Reason"));
    wprintf(L"Last updated (Unix seconds): %s\n",response.stringOf(L"SuppressedDestination.LastUpdateTime"));
    }