Sample code for 30+ languages & platforms
Dart

Amazon SES v2: Send a newsletter with an SES-managed topic unsubscribe link

See more Amazon SES Examples

Use an existing SES contact list named Readers with a ProductUpdates topic and a test subscriber who opted in. Replace the verified sender and subscriber address. Send to one recipient per message so SES can insert its subscription headers and personalize the footer link. When that reader opts out, SES honors the preference on future sends using the same list and topic.

SES subscription management · SendEmail

Chilkat Dart Downloads

Dart
import 'package:chilkat/chilkat.dart';

void main() {
  // Assumes Chilkat is unlocked. See Global Unlock Sample.

  final request = CkJsonObject();
  request.updateString('FromEmailAddress', 'sender@example.com');
  request.updateString('Destination.ToAddresses[0]', 'subscriber@example.com');
  request.updateString('ListManagementOptions.ContactListName', 'Readers');
  request.updateString('ListManagementOptions.TopicName', 'ProductUpdates');
  request.updateString('Content.Simple.Subject.Data', 'Product updates: November edition');
  request.updateString('Content.Simple.Body.Text.Data', 'The November catalog is available.\nManage your preferences: {{amazonSESUnsubscribeUrl}}');
  request.updateString('Content.Simple.Body.Html.Data', '<p>The November catalog is available.</p><p><a href="{{amazonSESUnsubscribeUrl}}">Manage your product update preferences</a></p>');
  // 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 response = CkJsonObject();
  try {
    response.load(responseText);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  print('SES accepted the message: ${response.stringOf('MessageId')}');
}