Sample code for 30+ languages & platforms
Delphi DLL

Amazon SES v2: Check an address on the account suppression list

See more Amazon SES Examples

Look up one address in the sending Region with ses:GetSuppressedDestination. The record distinguishes a bounce from a complaint. An absent record tells you only that this account suppression lookup found no entry. The example signs the exact encoded mailbox path and also works with temporary credentials.

GetSuppressedDestination

Chilkat Delphi DLL Downloads

Delphi DLL
var
success: Boolean;
address: PWideChar;
region: PWideChar;
accessKey: PWideChar;
secretKey: PWideChar;
sessionToken: PWideChar;
host: HCkStringBuilder;
mailbox: HCkStringBuilder;
path: HCkStringBuilder;
canonicalPath: HCkStringBuilder;
replaced: Integer;
clock: HCkDateTime;
stamp: HCkStringBuilder;
date: PWideChar;
rest: HCkRest;
headers: HCkStringBuilder;
signedHeaders: PWideChar;
crypto: HCkCrypt2;
canonical: HCkStringBuilder;
scope: HCkStringBuilder;
toSign: HCkStringBuilder;
seed: HCkStringBuilder;
key: PWideChar;
signature: PWideChar;
authorization: HCkStringBuilder;
connection: HCkSocket;
responseText: PWideChar;
response: HCkJsonObject;

begin
success := False;

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

//  Look up the address exactly as stored, including any plus tag.
address := 'avery+orders@example.com';
region := 'us-west-2';
accessKey := 'AWS_ACCESS_KEY_ID';
secretKey := 'AWS_SECRET_ACCESS_KEY';
sessionToken := '';
host := CkStringBuilder_Create();
CkStringBuilder_Append(host,'email.');
CkStringBuilder_Append(host,region);
CkStringBuilder_Append(host,'.amazonaws.com');
mailbox := CkStringBuilder_Create();
CkStringBuilder_Append(mailbox,address);
path := CkStringBuilder_Create();
CkStringBuilder_Append(path,'/v2/email/suppression/addresses/');
CkStringBuilder_Append(path,CkStringBuilder__getEncoded(mailbox,'url_rfc3986','utf-8'));

//  SigV4 for SES encodes the wire path a second time in the canonical request.
//  Only the canonical copy escapes the percent signs. Send the original path over HTTPS.
canonicalPath := CkStringBuilder_Create();
CkStringBuilder_Append(canonicalPath,CkStringBuilder__getAsString(path));
replaced := CkStringBuilder_Replace(canonicalPath,'%','%25');
clock := CkDateTime_Create();
CkDateTime_SetFromCurrentSystemTime(clock);
stamp := CkStringBuilder_Create();
CkStringBuilder_Append(stamp,CkDateTime__getAsTimestamp(clock,False));
replaced := CkStringBuilder_Replace(stamp,'-','');
replaced := CkStringBuilder_Replace(stamp,':','');
date := CkStringBuilder__getRange(stamp,0,8,False);
rest := CkRest_Create();
CkRest_AddHeader(rest,'Host',CkStringBuilder__getAsString(host));
CkRest_AddHeader(rest,'x-amz-date',CkStringBuilder__getAsString(stamp));
headers := CkStringBuilder_Create();
CkStringBuilder_Append(headers,'host:');
CkStringBuilder_Append(headers,CkStringBuilder__getAsString(host));
CkStringBuilder_Append(headers,#10 + 'x-amz-date:');
CkStringBuilder_Append(headers,CkStringBuilder__getAsString(stamp));
CkStringBuilder_Append(headers,#10);
signedHeaders := 'host;x-amz-date';
if (sessionToken <> '') then
  begin
    CkRest_AddHeader(rest,'x-amz-security-token',sessionToken);
    CkStringBuilder_Append(headers,'x-amz-security-token:');
    CkStringBuilder_Append(headers,sessionToken);
    CkStringBuilder_Append(headers,#10);
    signedHeaders := 'host;x-amz-date;x-amz-security-token';
  end;
crypto := CkCrypt2_Create();
CkCrypt2_putHashAlgorithm(crypto,'sha256');
CkCrypt2_putMacAlgorithm(crypto,'hmac');
CkCrypt2_putEncodingMode(crypto,'hexlower');
CkCrypt2_putCharset(crypto,'utf-8');
canonical := CkStringBuilder_Create();
CkStringBuilder_Append(canonical,'GET' + #10);
CkStringBuilder_Append(canonical,CkStringBuilder__getAsString(canonicalPath));
CkStringBuilder_Append(canonical,#10 + #10);
CkStringBuilder_Append(canonical,CkStringBuilder__getAsString(headers));
CkStringBuilder_Append(canonical,#10);
CkStringBuilder_Append(canonical,signedHeaders);
CkStringBuilder_Append(canonical,#10);
CkStringBuilder_Append(canonical,CkCrypt2__hashStringENC(crypto,''));
scope := CkStringBuilder_Create();
CkStringBuilder_Append(scope,date);
CkStringBuilder_Append(scope,'/');
CkStringBuilder_Append(scope,region);
CkStringBuilder_Append(scope,'/ses/aws4_request');
toSign := CkStringBuilder_Create();
CkStringBuilder_Append(toSign,'AWS4-HMAC-SHA256' + #10);
CkStringBuilder_Append(toSign,CkStringBuilder__getAsString(stamp));
CkStringBuilder_Append(toSign,#10);
CkStringBuilder_Append(toSign,CkStringBuilder__getAsString(scope));
CkStringBuilder_Append(toSign,#10);
CkStringBuilder_Append(toSign,CkCrypt2__hashStringENC(crypto,CkStringBuilder__getAsString(canonical)));

//  Derive the signing key one HMAC at a time. Decode each intermediate key from hex.
seed := CkStringBuilder_Create();
CkStringBuilder_Append(seed,'AWS4');
CkStringBuilder_Append(seed,secretKey);
CkCrypt2_SetMacKeyString(crypto,CkStringBuilder__getAsString(seed));
key := CkCrypt2__macStringENC(crypto,date);
CkCrypt2_SetMacKeyEncoded(crypto,key,'hex');
key := CkCrypt2__macStringENC(crypto,region);
CkCrypt2_SetMacKeyEncoded(crypto,key,'hex');
key := CkCrypt2__macStringENC(crypto,'ses');
CkCrypt2_SetMacKeyEncoded(crypto,key,'hex');
key := CkCrypt2__macStringENC(crypto,'aws4_request');
CkCrypt2_SetMacKeyEncoded(crypto,key,'hex');
signature := CkCrypt2__macStringENC(crypto,CkStringBuilder__getAsString(toSign));
authorization := CkStringBuilder_Create();
CkStringBuilder_Append(authorization,'AWS4-HMAC-SHA256 Credential=');
CkStringBuilder_Append(authorization,accessKey);
CkStringBuilder_Append(authorization,'/');
CkStringBuilder_Append(authorization,CkStringBuilder__getAsString(scope));
CkStringBuilder_Append(authorization,', SignedHeaders=');
CkStringBuilder_Append(authorization,signedHeaders);
CkStringBuilder_Append(authorization,', Signature=');
CkStringBuilder_Append(authorization,signature);
CkRest_AddHeader(rest,'Authorization',CkStringBuilder__getAsString(authorization));
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;
success := CkRest_UseConnection(rest,connection,False);
if (success <> True) then
  begin
    Memo1.Lines.Add(CkRest__lastErrorText(rest));
    Exit;
  end;

responseText := CkRest__fullRequestNoBody(rest,'GET',CkStringBuilder__getAsString(path));
if (CkRest_getLastMethodSuccess(rest) <> True) then
  begin
    Memo1.Lines.Add(CkRest__lastErrorText(rest));
    Exit;
  end;
if (CkRest_getResponseStatusCode(rest) = 404) then
  begin
    Memo1.Lines.Add('No account suppression entry for this address in this Region.');
    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(CkJsonObject__stringOf(response,'SuppressedDestination.EmailAddress'));
Memo1.Lines.Add('Reason: ' + CkJsonObject__stringOf(response,'SuppressedDestination.Reason'));
Memo1.Lines.Add('Last updated (Unix seconds): ' + CkJsonObject__stringOf(response,'SuppressedDestination.LastUpdateTime'));

CkStringBuilder_Dispose(host);
CkStringBuilder_Dispose(mailbox);
CkStringBuilder_Dispose(path);
CkStringBuilder_Dispose(canonicalPath);
CkDateTime_Dispose(clock);
CkStringBuilder_Dispose(stamp);
CkRest_Dispose(rest);
CkStringBuilder_Dispose(headers);
CkCrypt2_Dispose(crypto);
CkStringBuilder_Dispose(canonical);
CkStringBuilder_Dispose(scope);
CkStringBuilder_Dispose(toSign);
CkStringBuilder_Dispose(seed);
CkStringBuilder_Dispose(authorization);
CkSocket_Dispose(connection);
CkJsonObject_Dispose(response);