Unicode C
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
#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;
const wchar_t *domainName;
BOOL checkExisting;
const wchar_t *region;
HCkStringBuilderW host;
HCkAuthAwsW signer;
HCkSocketW connection;
HCkRestW rest;
const wchar_t *sessionToken;
const wchar_t *responseText;
HCkStringBuilderW path;
HCkJsonObjectW identity;
HCkJsonObjectW response;
int count;
const wchar_t *zone;
int i;
const wchar_t *token;
success = FALSE;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
success = FALSE;
domainName = L"example.com";
checkExisting = FALSE;
// 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);
}
responseText = L"";
if (checkExisting == TRUE) {
path = CkStringBuilderW_Create();
CkStringBuilderW_Append(path,L"/v2/email/identities/");
CkStringBuilderW_Append(path,domainName);
responseText = CkRestW_fullRequestNoBody(rest,L"GET",CkStringBuilderW_getAsString(path));
}
else {
identity = CkJsonObjectW_Create();
CkJsonObjectW_UpdateString(identity,L"EmailIdentity",domainName);
CkRestW_AddHeader(rest,L"Content-Type",L"application/json");
responseText = CkRestW_fullRequestString(rest,L"POST",L"/v2/email/identities",CkJsonObjectW_emit(identity));
}
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(identity);
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(identity);
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(identity);
CkJsonObjectW_Dispose(response);
return;
}
wprintf(L"DKIM status: %s\n",CkJsonObjectW_stringOf(response,L"DkimAttributes.Status"));
wprintf(L"Verified for sending: %d\n",CkJsonObjectW_BoolOf(response,L"VerifiedForSendingStatus"));
count = CkJsonObjectW_SizeOfArray(response,L"DkimAttributes.Tokens");
if (count < 1) {
wprintf(L"No Easy DKIM tokens were returned. Check this identity's DKIM configuration.\n");
CkStringBuilderW_Dispose(host);
CkAuthAwsW_Dispose(signer);
CkSocketW_Dispose(connection);
CkRestW_Dispose(rest);
CkStringBuilderW_Dispose(path);
CkJsonObjectW_Dispose(identity);
CkJsonObjectW_Dispose(response);
return;
}
if (CkJsonObjectW_HasMember(response,L"DkimAttributes.SigningHostedZone") != TRUE) {
wprintf(L"Find the CNAME targets for this identity in the SES console.\n");
CkStringBuilderW_Dispose(host);
CkAuthAwsW_Dispose(signer);
CkSocketW_Dispose(connection);
CkRestW_Dispose(rest);
CkStringBuilderW_Dispose(path);
CkJsonObjectW_Dispose(identity);
CkJsonObjectW_Dispose(response);
return;
}
zone = CkJsonObjectW_stringOf(response,L"DkimAttributes.SigningHostedZone");
i = 0;
while ((i < count)) {
CkJsonObjectW_putI(response,i);
token = CkJsonObjectW_stringOf(response,L"DkimAttributes.Tokens[i]");
wprintf(L"%s._domainkey.%s CNAME %s.%s\n",token,domainName,token,zone);
i = i + 1;
}
CkStringBuilderW_Dispose(host);
CkAuthAwsW_Dispose(signer);
CkSocketW_Dispose(connection);
CkRestW_Dispose(rest);
CkStringBuilderW_Dispose(path);
CkJsonObjectW_Dispose(identity);
CkJsonObjectW_Dispose(response);
}