Sample code for 30+ languages & platforms
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 C Downloads

C
#include <C_CkStringBuilder.h>
#include <C_CkAuthAws.h>
#include <C_CkSocket.h>
#include <C_CkRest.h>
#include <C_CkJsonObject.h>

void ChilkatSample(void)
    {
    BOOL success;
    const char *domainName;
    BOOL checkExisting;
    const char *region;
    HCkStringBuilder host;
    HCkAuthAws signer;
    HCkSocket connection;
    HCkRest rest;
    const char *sessionToken;
    const char *responseText;
    HCkStringBuilder path;
    HCkJsonObject identity;
    HCkJsonObject response;
    int count;
    const char *zone;
    int i;
    const char *token;

    success = FALSE;

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

    domainName = "example.com";
    checkExisting = FALSE;
    //  Use the same AWS Region for the identity, endpoint, and Signature Version 4.
    region = "us-west-2";
    host = CkStringBuilder_Create();
    CkStringBuilder_Append(host,"email.");
    CkStringBuilder_Append(host,region);
    CkStringBuilder_Append(host,".amazonaws.com");

    signer = CkAuthAws_Create();
    //  Supply the credentials from your application's credential provider.
    CkAuthAws_putAccessKey(signer,"AWS_ACCESS_KEY_ID");
    CkAuthAws_putSecretKey(signer,"AWS_SECRET_ACCESS_KEY");
    CkAuthAws_putRegion(signer,region);
    CkAuthAws_putServiceName(signer,"ses");

    connection = CkSocket_Create();
    CkSocket_putRequireSslCertVerify(connection,TRUE);
    success = CkSocket_Connect(connection,CkStringBuilder_getAsString(host),443,TRUE,10000);
    if (success != TRUE) {
        printf("%s\n",CkSocket_lastErrorText(connection));
        CkStringBuilder_Dispose(host);
        CkAuthAws_Dispose(signer);
        CkSocket_Dispose(connection);
        return;
    }

    rest = CkRest_Create();
    success = CkRest_UseConnection(rest,connection,FALSE);
    if (success != TRUE) {
        printf("%s\n",CkRest_lastErrorText(rest));
        CkStringBuilder_Dispose(host);
        CkAuthAws_Dispose(signer);
        CkSocket_Dispose(connection);
        CkRest_Dispose(rest);
        return;
    }

    success = CkRest_SetAuthAws(rest,signer);
    if (success != TRUE) {
        printf("%s\n",CkRest_lastErrorText(rest));
        CkStringBuilder_Dispose(host);
        CkAuthAws_Dispose(signer);
        CkSocket_Dispose(connection);
        CkRest_Dispose(rest);
        return;
    }

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

    responseText = "";
    if (checkExisting == TRUE) {
        path = CkStringBuilder_Create();
        CkStringBuilder_Append(path,"/v2/email/identities/");
        CkStringBuilder_Append(path,domainName);
        responseText = CkRest_fullRequestNoBody(rest,"GET",CkStringBuilder_getAsString(path));
    }
    else {
        identity = CkJsonObject_Create();
        CkJsonObject_UpdateString(identity,"EmailIdentity",domainName);
        CkRest_AddHeader(rest,"Content-Type","application/json");
        responseText = CkRest_fullRequestString(rest,"POST","/v2/email/identities",CkJsonObject_emit(identity));
    }

    if (CkRest_getLastMethodSuccess(rest) != TRUE) {
        printf("%s\n",CkRest_lastErrorText(rest));
        CkStringBuilder_Dispose(host);
        CkAuthAws_Dispose(signer);
        CkSocket_Dispose(connection);
        CkRest_Dispose(rest);
        CkStringBuilder_Dispose(path);
        CkJsonObject_Dispose(identity);
        return;
    }

    if (CkRest_getResponseStatusCode(rest) != 200) {
        printf("SES returned HTTP %d %s\n",CkRest_getResponseStatusCode(rest),responseText);
        CkStringBuilder_Dispose(host);
        CkAuthAws_Dispose(signer);
        CkSocket_Dispose(connection);
        CkRest_Dispose(rest);
        CkStringBuilder_Dispose(path);
        CkJsonObject_Dispose(identity);
        return;
    }

    response = CkJsonObject_Create();
    success = CkJsonObject_Load(response,responseText);
    if (success != TRUE) {
        printf("%s\n",CkJsonObject_lastErrorText(response));
        CkStringBuilder_Dispose(host);
        CkAuthAws_Dispose(signer);
        CkSocket_Dispose(connection);
        CkRest_Dispose(rest);
        CkStringBuilder_Dispose(path);
        CkJsonObject_Dispose(identity);
        CkJsonObject_Dispose(response);
        return;
    }

    printf("DKIM status: %s\n",CkJsonObject_stringOf(response,"DkimAttributes.Status"));
    printf("Verified for sending: %d\n",CkJsonObject_BoolOf(response,"VerifiedForSendingStatus"));
    count = CkJsonObject_SizeOfArray(response,"DkimAttributes.Tokens");
    if (count < 1) {
        printf("No Easy DKIM tokens were returned. Check this identity's DKIM configuration.\n");
        CkStringBuilder_Dispose(host);
        CkAuthAws_Dispose(signer);
        CkSocket_Dispose(connection);
        CkRest_Dispose(rest);
        CkStringBuilder_Dispose(path);
        CkJsonObject_Dispose(identity);
        CkJsonObject_Dispose(response);
        return;
    }

    if (CkJsonObject_HasMember(response,"DkimAttributes.SigningHostedZone") != TRUE) {
        printf("Find the CNAME targets for this identity in the SES console.\n");
        CkStringBuilder_Dispose(host);
        CkAuthAws_Dispose(signer);
        CkSocket_Dispose(connection);
        CkRest_Dispose(rest);
        CkStringBuilder_Dispose(path);
        CkJsonObject_Dispose(identity);
        CkJsonObject_Dispose(response);
        return;
    }

    zone = CkJsonObject_stringOf(response,"DkimAttributes.SigningHostedZone");
    i = 0;
    while ((i < count)) {
        CkJsonObject_putI(response,i);
        token = CkJsonObject_stringOf(response,"DkimAttributes.Tokens[i]");
        printf("%s._domainkey.%s CNAME %s.%s\n",token,domainName,token,zone);
        i = i + 1;
    }



    CkStringBuilder_Dispose(host);
    CkAuthAws_Dispose(signer);
    CkSocket_Dispose(connection);
    CkRest_Dispose(rest);
    CkStringBuilder_Dispose(path);
    CkJsonObject_Dispose(identity);
    CkJsonObject_Dispose(response);

    }