Objective-C
Objective-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.
Chilkat Objective-C Downloads
#import <NSString.h>
#import <CkoStringBuilder.h>
#import <CkoDateTime.h>
#import <CkoRest.h>
#import <CkoCrypt2.h>
#import <CkoSocket.h>
#import <CkoJsonObject.h>
BOOL success = NO;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
success = NO;
// Look up the address exactly as stored, including any plus tag.
NSString *address = @"avery+orders@example.com";
NSString *region = @"us-west-2";
NSString *accessKey = @"AWS_ACCESS_KEY_ID";
NSString *secretKey = @"AWS_SECRET_ACCESS_KEY";
NSString *sessionToken = @"";
CkoStringBuilder *host = [[CkoStringBuilder alloc] init];
[host Append: @"email."];
[host Append: region];
[host Append: @".amazonaws.com"];
CkoStringBuilder *mailbox = [[CkoStringBuilder alloc] init];
[mailbox Append: address];
CkoStringBuilder *path = [[CkoStringBuilder alloc] init];
[path Append: @"/v2/email/suppression/addresses/"];
[path Append: [mailbox GetEncoded: @"url_rfc3986" charset: @"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.
CkoStringBuilder *canonicalPath = [[CkoStringBuilder alloc] init];
[canonicalPath Append: [path GetAsString]];
int replaced = [[canonicalPath Replace: @"%" replacement: @"%25"] intValue];
CkoDateTime *clock = [[CkoDateTime alloc] init];
[clock SetFromCurrentSystemTime];
CkoStringBuilder *stamp = [[CkoStringBuilder alloc] init];
[stamp Append: [clock GetAsTimestamp: NO]];
replaced = [[stamp Replace: @"-" replacement: @""] intValue];
replaced = [[stamp Replace: @":" replacement: @""] intValue];
NSString *date = [stamp GetRange: [NSNumber numberWithInt: 0] numChars: [NSNumber numberWithInt: 8] removeFlag: NO];
CkoRest *rest = [[CkoRest alloc] init];
[rest AddHeader: @"Host" value: [host GetAsString]];
[rest AddHeader: @"x-amz-date" value: [stamp GetAsString]];
CkoStringBuilder *headers = [[CkoStringBuilder alloc] init];
[headers Append: @"host:"];
[headers Append: [host GetAsString]];
[headers Append: @"\nx-amz-date:"];
[headers Append: [stamp GetAsString]];
[headers Append: @"\n"];
NSString *signedHeaders = @"host;x-amz-date";
if (sessionToken != @"") {
[rest AddHeader: @"x-amz-security-token" value: sessionToken];
[headers Append: @"x-amz-security-token:"];
[headers Append: sessionToken];
[headers Append: @"\n"];
signedHeaders = @"host;x-amz-date;x-amz-security-token";
}
CkoCrypt2 *crypto = [[CkoCrypt2 alloc] init];
crypto.HashAlgorithm = @"sha256";
crypto.MacAlgorithm = @"hmac";
crypto.EncodingMode = @"hexlower";
crypto.Charset = @"utf-8";
CkoStringBuilder *canonical = [[CkoStringBuilder alloc] init];
[canonical Append: @"GET\n"];
[canonical Append: [canonicalPath GetAsString]];
[canonical Append: @"\n\n"];
[canonical Append: [headers GetAsString]];
[canonical Append: @"\n"];
[canonical Append: signedHeaders];
[canonical Append: @"\n"];
[canonical Append: [crypto HashStringENC: @""]];
CkoStringBuilder *scope = [[CkoStringBuilder alloc] init];
[scope Append: date];
[scope Append: @"/"];
[scope Append: region];
[scope Append: @"/ses/aws4_request"];
CkoStringBuilder *toSign = [[CkoStringBuilder alloc] init];
[toSign Append: @"AWS4-HMAC-SHA256\n"];
[toSign Append: [stamp GetAsString]];
[toSign Append: @"\n"];
[toSign Append: [scope GetAsString]];
[toSign Append: @"\n"];
[toSign Append: [crypto HashStringENC: [canonical GetAsString]]];
// Derive the signing key one HMAC at a time. Decode each intermediate key from hex.
CkoStringBuilder *seed = [[CkoStringBuilder alloc] init];
[seed Append: @"AWS4"];
[seed Append: secretKey];
[crypto SetMacKeyString: [seed GetAsString]];
NSString *key = [crypto MacStringENC: date];
[crypto SetMacKeyEncoded: key encoding: @"hex"];
key = [crypto MacStringENC: region];
[crypto SetMacKeyEncoded: key encoding: @"hex"];
key = [crypto MacStringENC: @"ses"];
[crypto SetMacKeyEncoded: key encoding: @"hex"];
key = [crypto MacStringENC: @"aws4_request"];
[crypto SetMacKeyEncoded: key encoding: @"hex"];
NSString *signature = [crypto MacStringENC: [toSign GetAsString]];
CkoStringBuilder *authorization = [[CkoStringBuilder alloc] init];
[authorization Append: @"AWS4-HMAC-SHA256 Credential="];
[authorization Append: accessKey];
[authorization Append: @"/"];
[authorization Append: [scope GetAsString]];
[authorization Append: @", SignedHeaders="];
[authorization Append: signedHeaders];
[authorization Append: @", Signature="];
[authorization Append: signature];
[rest AddHeader: @"Authorization" value: [authorization GetAsString]];
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;
}
success = [rest UseConnection: connection autoReconnect: NO];
if (success != YES) {
NSLog(@"%@",rest.LastErrorText);
return;
}
NSString *responseText = [rest FullRequestNoBody: @"GET" uriPath: [path GetAsString]];
if (rest.LastMethodSuccess != YES) {
NSLog(@"%@",rest.LastErrorText);
return;
}
if ([rest.ResponseStatusCode intValue] == 404) {
NSLog(@"%@",@"No account suppression entry for this address in this Region.");
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(@"%@",[response StringOf: @"SuppressedDestination.EmailAddress"]);
NSLog(@"%@%@",@"Reason: ",[response StringOf: @"SuppressedDestination.Reason"]);
NSLog(@"%@%@",@"Last updated (Unix seconds): ",[response StringOf: @"SuppressedDestination.LastUpdateTime"]);