Objective-C
Objective-C
Amazon SES v2: Attach a PDF to a simple email
See more Amazon SES Examples
Place invoice.pdf in the working directory and use a verified sender. RawContent contains the Base64 representation of the file bytes for JSON transport. ContentTransferEncoding tells SES how to encode the PDF when it assembles the outgoing MIME message. For custom MIME structure, see Send a raw MIME email.
Chilkat Objective-C Downloads
#import <CkoBinData.h>
#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;
CkoBinData *pdf = [[CkoBinData alloc] init];
success = [pdf LoadFile: @"invoice.pdf"];
if (success != YES) {
NSLog(@"%@",pdf.LastErrorText);
return;
}
if ([pdf.NumBytes intValue] == 0) {
NSLog(@"%@",@"invoice.pdf is empty.");
return;
}
CkoJsonObject *request = [[CkoJsonObject alloc] init];
[request UpdateString: @"FromEmailAddress" value: @"sender@example.com"];
[request UpdateString: @"Destination.ToAddresses[0]" value: @"success@simulator.amazonses.com"];
[request UpdateString: @"Content.Simple.Subject.Data" value: @"Invoice 1042"];
[request UpdateString: @"Content.Simple.Body.Text.Data" value: @"Your invoice is attached."];
[request UpdateString: @"Content.Simple.Attachments[0].FileName" value: @"invoice-1042.pdf"];
[request UpdateString: @"Content.Simple.Attachments[0].ContentType" value: @"application/pdf"];
[request UpdateString: @"Content.Simple.Attachments[0].ContentDisposition" value: @"ATTACHMENT"];
[request UpdateString: @"Content.Simple.Attachments[0].ContentTransferEncoding" value: @"BASE64"];
[request UpdateString: @"Content.Simple.Attachments[0].RawContent" value: [pdf GetEncoded: @"base64"]];
// 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-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;
}
NSLog(@"%@%@",@"SES accepted the message: ",[response StringOf: @"MessageId"]);