Dart
Dart
Amazon SES v2: Send a plain text email
See more Amazon SES Examples
Send a plain text confirmation using a verified sender and credentials with ses:SendEmail. The SES mailbox simulator also works in the sandbox. Other sandbox recipients must be verified. SES permits up to 50 recipients total across To, CC and BCC. Save the returned message ID to correlate later delivery events.
SendEmail · SES sandbox · Sending quotas
Chilkat Dart Downloads
import 'package:chilkat/chilkat.dart';
void main() {
// Assumes Chilkat is unlocked. See Global Unlock Sample.
// Replace the From address with your verified sender. The destination is the SES mailbox simulator.
final request = CkJsonObject();
request.updateString('FromEmailAddress', 'sender+reports@example.com');
request.updateString('Destination.ToAddresses[0]', 'success@simulator.amazonses.com');
request.updateString('Content.Simple.Subject.Data', 'Report ready');
request.updateString('Content.Simple.Subject.Charset', 'UTF-8');
request.updateString('Content.Simple.Body.Text.Data', 'Your weekly report is ready to view.');
request.updateString('Content.Simple.Body.Text.Charset', 'UTF-8');
// 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')}');
}