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

Amazon SES v2: Verify a sending domain with Easy DKIM

See more Amazon SES Examples

Use a domain you control. Run once to create the regional identity, publish the returned CNAMEs with your DNS provider, then set checkExisting to true and run again to read the status. The target suffix comes from SES. If your DNS provider appends the domain automatically, enter just the relative record name.

CreateEmailIdentity · GetEmailIdentity · Easy DKIM records

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;

    const wchar_t *domainName = L"example.com";
    bool checkExisting = false;
    //  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);
    }

    const wchar_t *responseText = L"";
    if (checkExisting == true) {
        CkStringBuilderW path;
        path.Append(L"/v2/email/identities/");
        path.Append(domainName);
        responseText = rest.fullRequestNoBody(L"GET",path.getAsString());
    }
    else {
        CkJsonObjectW identity;
        identity.UpdateString(L"EmailIdentity",domainName);
        rest.AddHeader(L"Content-Type",L"application/json");
        responseText = rest.fullRequestString(L"POST",L"/v2/email/identities",identity.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"DKIM status: %s\n",response.stringOf(L"DkimAttributes.Status"));
    wprintf(L"Verified for sending: %d\n",response.BoolOf(L"VerifiedForSendingStatus"));
    int count = response.SizeOfArray(L"DkimAttributes.Tokens");
    if (count < 1) {
        wprintf(L"No Easy DKIM tokens were returned. Check this identity's DKIM configuration.\n");
        return;
    }

    if (response.HasMember(L"DkimAttributes.SigningHostedZone") != true) {
        wprintf(L"Find the CNAME targets for this identity in the SES console.\n");
        return;
    }

    const wchar_t *zone = response.stringOf(L"DkimAttributes.SigningHostedZone");
    int i = 0;
    while ((i < count)) {
        response.put_I(i);
        const wchar_t *token = response.stringOf(L"DkimAttributes.Tokens[i]");
        wprintf(L"%s._domainkey.%s CNAME %s.%s\n",token,domainName,token,zone);
        i = i + 1;
    }
    }