Objective-C
Objective-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 Objective-C Downloads
#import <NSString.h>
#import <CkoStringBuilder.h>
#import <CkoAuthAws.h>
#import <CkoSocket.h>
#import <CkoRest.h>
#import <CkoJsonObject.h>
BOOL success = NO;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
success = NO;
NSString *domainName = @"example.com";
BOOL checkExisting = NO;
// Use the same AWS Region for the identity, endpoint, and Signature Version 4.
NSString *region = @"us-west-2";
CkoStringBuilder *host = [[CkoStringBuilder alloc] init];
[host Append: @"email."];
[host Append: region];
[host Append: @".amazonaws.com"];
CkoAuthAws *signer = [[CkoAuthAws alloc] init];
// Supply the credentials from your application's credential provider.
signer.AccessKey = @"AWS_ACCESS_KEY_ID";
signer.SecretKey = @"AWS_SECRET_ACCESS_KEY";
signer.Region = region;
signer.ServiceName = @"ses";
CkoSocket *connection = [[CkoSocket alloc] init];
connection.RequireSslCertVerify = YES;
success = [connection Connect: [host GetAsString] port: [NSNumber numberWithInt: 443] ssl: YES maxWaitMs: [NSNumber numberWithInt: 10000]];
if (success != YES) {
NSLog(@"%@",connection.LastErrorText);
return;
}
CkoRest *rest = [[CkoRest alloc] init];
success = [rest UseConnection: connection autoReconnect: NO];
if (success != YES) {
NSLog(@"%@",rest.LastErrorText);
return;
}
success = [rest SetAuthAws: signer];
if (success != YES) {
NSLog(@"%@",rest.LastErrorText);
return;
}
// For temporary AWS credentials, supply the session token from the same provider.
NSString *sessionToken = @"";
if (sessionToken != @"") {
[rest AddQueryParam: @"X-Amz-Security-Token" value: sessionToken];
}
NSString *responseText = @"";
if (checkExisting == YES) {
CkoStringBuilder *path = [[CkoStringBuilder alloc] init];
[path Append: @"/v2/email/identities/"];
[path Append: domainName];
responseText = [rest FullRequestNoBody: @"GET" uriPath: [path GetAsString]];
}
else {
CkoJsonObject *identity = [[CkoJsonObject alloc] init];
[identity UpdateString: @"EmailIdentity" value: domainName];
[rest AddHeader: @"Content-Type" value: @"application/json"];
responseText = [rest FullRequestString: @"POST" uriPath: @"/v2/email/identities" bodyText: [identity Emit]];
}
if (rest.LastMethodSuccess != YES) {
NSLog(@"%@",rest.LastErrorText);
return;
}
if ([rest.ResponseStatusCode intValue] != 200) {
NSLog(@"%@%d%@%@",@"SES returned HTTP ",[rest.ResponseStatusCode intValue],@" ",responseText);
return;
}
CkoJsonObject *response = [[CkoJsonObject alloc] init];
success = [response Load: responseText];
if (success != YES) {
NSLog(@"%@",response.LastErrorText);
return;
}
NSLog(@"%@%@",@"DKIM status: ",[response StringOf: @"DkimAttributes.Status"]);
NSLog(@"%@%d",@"Verified for sending: ",[response BoolOf: @"VerifiedForSendingStatus"]);
int count = [[response SizeOfArray: @"DkimAttributes.Tokens"] intValue];
if (count < 1) {
NSLog(@"%@",@"No Easy DKIM tokens were returned. Check this identity's DKIM configuration.");
return;
}
if ([response HasMember: @"DkimAttributes.SigningHostedZone"] != YES) {
NSLog(@"%@",@"Find the CNAME targets for this identity in the SES console.");
return;
}
NSString *zone = [response StringOf: @"DkimAttributes.SigningHostedZone"];
int i = 0;
while ((i < count)) {
response.I = [NSNumber numberWithInt: i];
NSString *token = [response StringOf: @"DkimAttributes.Tokens[i]"];
NSLog(@"%@%@%@%@%@%@%@",token,@"._domainkey.",domainName,@" CNAME ",token,@".",zone);
i = i + 1;
}