Dart
Dart
Amazon SES v2: Send a raw MIME email with a PDF attachment
See more Amazon SES Examples
Place invoice.pdf in the working directory. Chilkat builds the MIME message and gives the attachment the name invoice-1042.pdf. The v2 SendEmail request carries those bytes in Content.Raw.Data. For a blind copy, supply the address in Destination.BccAddresses and keep it out of the MIME headers.
Use a verified sender and grant ses:SendEmail for this v2 operation. The v2 message size limit is 40 MB including attachments after MIME encoding. For a straightforward attachment that needs no custom MIME layout, v2 also supports Content.Simple.Attachments.
Sending raw email · SES attachments · Sending quotas
Chilkat Dart Downloads
import 'package:chilkat/chilkat.dart';
void main() {
// Assumes Chilkat is unlocked. See Global Unlock Sample.
// Read the PDF before making a network request.
final pdf = CkBinData();
try {
pdf.loadFile('invoice.pdf');
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
if (pdf.numBytes == 0) {
print('invoice.pdf is empty.');
return;
}
final sender = 'sender+reports@example.com';
final recipient = 'success@simulator.amazonses.com';
final email = CkEmail();
email.from = sender;
email.subject = 'Invoice 1042';
email.charset = 'utf-8';
try {
email.addTo('', recipient);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
email.addPlainTextAlternativeBody('Invoice 1042 is attached.');
email.addHtmlAlternativeBody('<p>Invoice <strong>1042</strong> is attached.</p>');
try {
email.addAttachmentBd('invoice-1042.pdf', pdf, 'application/pdf');
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
// Get the exact MIME bytes, including the attachment's transfer encoding.
final mime = CkBinData();
try {
email.getMimeBd(mime);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
// Base64 here transports the entire MIME message as a blob in the JSON request.
final request = CkJsonObject();
request.updateString('FromEmailAddress', sender);
request.updateString('Destination.ToAddresses[0]', recipient);
request.updateString('Content.Raw.Data', mime.getEncoded('base64'));
// Use the same AWS Region for the identity, endpoint, and Signature Version 4.
final region = 'us-west-2';
final host = CkStringBuilder();
host.append('email.');
host.append(region);
host.append('.amazonaws.com');
final signer = CkAuthAws();
// 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';
final connection = CkSocket();
connection.requireSslCertVerify = true;
try {
connection.connect(host.getAsString(), 443, true, 10000);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
final rest = CkRest();
try {
rest.useConnection(connection, false);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
try {
rest.setAuthAws(signer);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
// For temporary AWS credentials, supply the session token from the same provider.
final sessionToken = '';
if (sessionToken != '') {
rest.addQueryParam('X-Amz-Security-Token', sessionToken);
}
rest.addHeader('Content-Type', 'application/json');
final String responseText;
try {
responseText = rest.fullRequestString('POST', '/v2/email/outbound-emails', request.emit());
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
if (rest.responseStatusCode != 200) {
print('SES returned HTTP ${rest.responseStatusCode} $responseText');
return;
}
final result = CkJsonObject();
try {
result.load(responseText);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
if (!result.hasMember('MessageId')) {
print('SES returned no message ID.');
return;
}
print('SES accepted the message. Message ID: ${result.stringOf('MessageId')}');
}