Objective-C
Objective-C
Amazon SES v2: Send private appointment reminders and inspect each result
See more Amazon SES Examples
Replace the two recipients with test mailboxes you control. The example supplies an inline template and one private destination per entry. Store each successful message ID against its appointment. Schedule a retry with backoff only for entries with a retryable outcome. Correct permanent errors before resubmission.
SendBulkEmail · Per-entry outcomes
Chilkat Objective-C Downloads
#import <CkoJsonObject.h>
#import <NSString.h>
#import <CkoStringBuilder.h>
#import <CkoAuthAws.h>
#import <CkoSocket.h>
#import <CkoRest.h>
BOOL success = NO;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
success = NO;
CkoJsonObject *request = [[CkoJsonObject alloc] init];
[request UpdateString: @"FromEmailAddress" value: @"sender@example.com"];
[request UpdateString: @"DefaultContent.Template.TemplateContent.Subject" value: @"Appointment reminder"];
[request UpdateString: @"DefaultContent.Template.TemplateContent.Text" value: @"Hello {{name}}, your appointment is {{slot}}."];
[request UpdateString: @"DefaultContent.Template.TemplateData" value: @"{\"name\":\"there\",\"slot\":\"in your booking confirmation\"}"];
CkoJsonObject *firstReminder = [[CkoJsonObject alloc] init];
[firstReminder UpdateString: @"name" value: @"Avery"];
[firstReminder UpdateString: @"slot" value: @"Tuesday at 9 AM Central"];
[request UpdateString: @"BulkEmailEntries[0].Destination.ToAddresses[0]" value: @"avery@example.com"];
[request UpdateString: @"BulkEmailEntries[0].ReplacementEmailContent.ReplacementTemplate.ReplacementTemplateData" value: [firstReminder Emit]];
CkoJsonObject *secondReminder = [[CkoJsonObject alloc] init];
[secondReminder UpdateString: @"name" value: @"Robin"];
[secondReminder UpdateString: @"slot" value: @"Wednesday at 2 PM Central"];
[request UpdateString: @"BulkEmailEntries[1].Destination.ToAddresses[0]" value: @"robin@example.com"];
[request UpdateString: @"BulkEmailEntries[1].ReplacementEmailContent.ReplacementTemplate.ReplacementTemplateData" value: [secondReminder Emit]];
// 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];
}
[rest AddHeader: @"Content-Type" value: @"application/json"];
NSString *responseText = [rest FullRequestString: @"POST" uriPath: @"/v2/email/outbound-bulk-emails" bodyText: [request 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;
}
int count = [[response SizeOfArray: @"BulkEmailEntryResults"] intValue];
if (count != [[request SizeOfArray: @"BulkEmailEntries"] intValue]) {
NSLog(@"%@",@"Incomplete results. Reconcile the original batch before retrying.");
return;
}
int i = 0;
while ((i < count)) {
response.I = [NSNumber numberWithInt: i];
request.I = [NSNumber numberWithInt: i];
NSString *recipient = [request StringOf: @"BulkEmailEntries[i].Destination.ToAddresses[0]"];
NSString *status = [response StringOf: @"BulkEmailEntryResults[i].Status"];
if (status == @"SUCCESS") {
NSLog(@"%@%@%@",recipient,@" accepted: ",[response StringOf: @"BulkEmailEntryResults[i].MessageId"]);
}
else {
if ((status == @"ACCOUNT_THROTTLED") || (status == @"TRANSIENT_FAILURE")) {
NSLog(@"%@%@%@",recipient,@" retry later: ",status);
}
else {
NSLog(@"%@%@%@%@%@",recipient,@" review before retry: ",status,@" ",[response StringOf: @"BulkEmailEntryResults[i].Error"]);
}
}
i = i + 1;
}