Sample code for 30+ languages & platforms
Delphi DLL

Amazon SES v2: Publish delivery events to SNS and correlate a tagged send

See more Amazon SES Examples

Use an existing configuration set and Standard SNS topic in the same Region. Grant SES permission to publish to the topic and subscribe your consumer. Configure the destination once, then set configureDestination to false to send a test. In the SES event inside the SNS Message, eventType identifies the outcome, mail.messageId matches the send result, and mail.tags.notification_ref[0] identifies the application record. Use the SES bounce or complaint simulator address to exercise those outcomes.

Configure the SNS destination · SES event payload

Chilkat Delphi DLL Downloads

Delphi DLL
var
success: Boolean;
configureDestination: Boolean;
setName: PWideChar;
region: PWideChar;
host: HCkStringBuilder;
signer: HCkAuthAws;
connection: HCkSocket;
rest: HCkRest;
sessionToken: PWideChar;
path: HCkStringBuilder;
destination: HCkJsonObject;
setupResponse: PWideChar;
request: HCkJsonObject;
responseText: PWideChar;
response: HCkJsonObject;

begin
success := False;

//  Assumes Chilkat is unlocked. See Global Unlock Sample.
success := False;

configureDestination := True;
setName := 'application-mail';
//  Use the same AWS Region for the identity, endpoint, and Signature Version 4.
region := 'us-west-2';
host := CkStringBuilder_Create();
CkStringBuilder_Append(host,'email.');
CkStringBuilder_Append(host,region);
CkStringBuilder_Append(host,'.amazonaws.com');

signer := CkAuthAws_Create();
//  Supply the credentials from your application's credential provider.
CkAuthAws_putAccessKey(signer,'AWS_ACCESS_KEY_ID');
CkAuthAws_putSecretKey(signer,'AWS_SECRET_ACCESS_KEY');
CkAuthAws_putRegion(signer,region);
CkAuthAws_putServiceName(signer,'ses');

connection := CkSocket_Create();
CkSocket_putRequireSslCertVerify(connection,True);
success := CkSocket_Connect(connection,CkStringBuilder__getAsString(host),443,True,10000);
if (success <> True) then
  begin
    Memo1.Lines.Add(CkSocket__lastErrorText(connection));
    Exit;
  end;

rest := CkRest_Create();
success := CkRest_UseConnection(rest,connection,False);
if (success <> True) then
  begin
    Memo1.Lines.Add(CkRest__lastErrorText(rest));
    Exit;
  end;
success := CkRest_SetAuthAws(rest,signer);
if (success <> True) then
  begin
    Memo1.Lines.Add(CkRest__lastErrorText(rest));
    Exit;
  end;
//  For temporary AWS credentials, supply the session token from the same provider.
sessionToken := '';
if (sessionToken <> '') then
  begin
    CkRest_AddQueryParam(rest,'X-Amz-Security-Token',sessionToken);
  end;
if (configureDestination = True) then
  begin
    path := CkStringBuilder_Create();
    CkStringBuilder_Append(path,'/v2/email/configuration-sets/');
    CkStringBuilder_Append(path,setName);
    CkStringBuilder_Append(path,'/event-destinations');
    destination := CkJsonObject_Create();
    CkJsonObject_UpdateString(destination,'EventDestinationName','delivery-to-sns');
    CkJsonObject_UpdateString(destination,'EventDestination.MatchingEventTypes[0]','DELIVERY');
    CkJsonObject_UpdateString(destination,'EventDestination.MatchingEventTypes[1]','BOUNCE');
    CkJsonObject_UpdateString(destination,'EventDestination.MatchingEventTypes[2]','COMPLAINT');
    CkJsonObject_UpdateString(destination,'EventDestination.SnsDestination.TopicArn','arn:aws:sns:us-west-2:123456789012:ses-delivery');
    CkJsonObject_UpdateBool(destination,'EventDestination.Enabled',True);

    CkRest_AddHeader(rest,'Content-Type','application/json');
    setupResponse := CkRest__fullRequestString(rest,'POST',CkStringBuilder__getAsString(path),CkJsonObject__emit(destination));
    if (CkRest_getLastMethodSuccess(rest) <> True) then
      begin
        Memo1.Lines.Add(CkRest__lastErrorText(rest));
        Exit;
      end;
    if (CkRest_getResponseStatusCode(rest) <> 200) then
      begin
        Memo1.Lines.Add('SES returned HTTP ' + IntToStr(CkRest_getResponseStatusCode(rest)) + ' ' + setupResponse);
        Exit;
      end;
    Memo1.Lines.Add('Destination created. Use the send branch to test it.');
    Exit;
  end;
request := CkJsonObject_Create();
CkJsonObject_UpdateString(request,'FromEmailAddress','sender@example.com');
CkJsonObject_UpdateString(request,'Destination.ToAddresses[0]','success@simulator.amazonses.com');
CkJsonObject_UpdateString(request,'Content.Simple.Subject.Data','Delivery event test');
CkJsonObject_UpdateString(request,'Content.Simple.Body.Text.Data','Exercise the application-mail configuration set.');
CkJsonObject_UpdateString(request,'EmailTags[0].Name','notification_ref');
CkJsonObject_UpdateString(request,'EmailTags[0].Value','notify-1042');
CkJsonObject_UpdateString(request,'ConfigurationSetName',setName);

CkRest_AddHeader(rest,'Content-Type','application/json');
responseText := CkRest__fullRequestString(rest,'POST','/v2/email/outbound-emails',CkJsonObject__emit(request));
if (CkRest_getLastMethodSuccess(rest) <> True) then
  begin
    Memo1.Lines.Add(CkRest__lastErrorText(rest));
    Exit;
  end;
if (CkRest_getResponseStatusCode(rest) <> 200) then
  begin
    Memo1.Lines.Add('SES returned HTTP ' + IntToStr(CkRest_getResponseStatusCode(rest)) + ' ' + responseText);
    Exit;
  end;
response := CkJsonObject_Create();
success := CkJsonObject_Load(response,responseText);
if (success <> True) then
  begin
    Memo1.Lines.Add(CkJsonObject__lastErrorText(response));
    Exit;
  end;
Memo1.Lines.Add('SES accepted the message: ' + CkJsonObject__stringOf(response,'MessageId'));

CkStringBuilder_Dispose(host);
CkAuthAws_Dispose(signer);
CkSocket_Dispose(connection);
CkRest_Dispose(rest);
    CkStringBuilder_Dispose(path);
    CkJsonObject_Dispose(destination);
CkJsonObject_Dispose(request);
CkJsonObject_Dispose(response);