Sample code for 30+ languages & platforms
CkJava

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 CkJava Downloads

CkJava
import com.chilkatsoft.*;

public class ChilkatExample {

  static {
    try {
        System.loadLibrary("chilkat");
    } catch (UnsatisfiedLinkError e) {
      System.err.println("Native code library failed to load.\n" + e);
      System.exit(1);
    }
  }

  public static void main(String argv[])
  {
    boolean success = false;

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

    //  Look up the address exactly as stored, including any plus tag.
    String address = "avery+orders@example.com";
    String region = "us-west-2";
    String accessKey = "AWS_ACCESS_KEY_ID";
    String secretKey = "AWS_SECRET_ACCESS_KEY";
    String sessionToken = "";
    CkStringBuilder host = new CkStringBuilder();
    host.Append("email.");
    host.Append(region);
    host.Append(".amazonaws.com");
    CkStringBuilder mailbox = new CkStringBuilder();
    mailbox.Append(address);
    CkStringBuilder path = new CkStringBuilder();
    path.Append("/v2/email/suppression/addresses/");
    path.Append(mailbox.getEncoded("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.
    CkStringBuilder canonicalPath = new CkStringBuilder();
    canonicalPath.Append(path.getAsString());
    int replaced = canonicalPath.Replace("%","%25");
    CkDateTime clock = new CkDateTime();
    clock.SetFromCurrentSystemTime();
    CkStringBuilder stamp = new CkStringBuilder();
    stamp.Append(clock.getAsTimestamp(false));
    replaced = stamp.Replace("-","");
    replaced = stamp.Replace(":","");
    String date = stamp.getRange(0,8,false);
    CkRest rest = new CkRest();
    rest.AddHeader("Host",host.getAsString());
    rest.AddHeader("x-amz-date",stamp.getAsString());
    CkStringBuilder headers = new CkStringBuilder();
    headers.Append("host:");
    headers.Append(host.getAsString());
    headers.Append("\nx-amz-date:");
    headers.Append(stamp.getAsString());
    headers.Append("\n");
    String signedHeaders = "host;x-amz-date";
    if (sessionToken != "") {
        rest.AddHeader("x-amz-security-token",sessionToken);
        headers.Append("x-amz-security-token:");
        headers.Append(sessionToken);
        headers.Append("\n");
        signedHeaders = "host;x-amz-date;x-amz-security-token";
        }

    CkCrypt2 crypto = new CkCrypt2();
    crypto.put_HashAlgorithm("sha256");
    crypto.put_MacAlgorithm("hmac");
    crypto.put_EncodingMode("hexlower");
    crypto.put_Charset("utf-8");
    CkStringBuilder canonical = new CkStringBuilder();
    canonical.Append("GET\n");
    canonical.Append(canonicalPath.getAsString());
    canonical.Append("\n\n");
    canonical.Append(headers.getAsString());
    canonical.Append("\n");
    canonical.Append(signedHeaders);
    canonical.Append("\n");
    canonical.Append(crypto.hashStringENC(""));
    CkStringBuilder scope = new CkStringBuilder();
    scope.Append(date);
    scope.Append("/");
    scope.Append(region);
    scope.Append("/ses/aws4_request");
    CkStringBuilder toSign = new CkStringBuilder();
    toSign.Append("AWS4-HMAC-SHA256\n");
    toSign.Append(stamp.getAsString());
    toSign.Append("\n");
    toSign.Append(scope.getAsString());
    toSign.Append("\n");
    toSign.Append(crypto.hashStringENC(canonical.getAsString()));

    //  Derive the signing key one HMAC at a time. Decode each intermediate key from hex.
    CkStringBuilder seed = new CkStringBuilder();
    seed.Append("AWS4");
    seed.Append(secretKey);
    crypto.SetMacKeyString(seed.getAsString());
    String key = crypto.macStringENC(date);
    crypto.SetMacKeyEncoded(key,"hex");
    key = crypto.macStringENC(region);
    crypto.SetMacKeyEncoded(key,"hex");
    key = crypto.macStringENC("ses");
    crypto.SetMacKeyEncoded(key,"hex");
    key = crypto.macStringENC("aws4_request");
    crypto.SetMacKeyEncoded(key,"hex");
    String signature = crypto.macStringENC(toSign.getAsString());
    CkStringBuilder authorization = new CkStringBuilder();
    authorization.Append("AWS4-HMAC-SHA256 Credential=");
    authorization.Append(accessKey);
    authorization.Append("/");
    authorization.Append(scope.getAsString());
    authorization.Append(", SignedHeaders=");
    authorization.Append(signedHeaders);
    authorization.Append(", Signature=");
    authorization.Append(signature);
    rest.AddHeader("Authorization",authorization.getAsString());
    CkSocket connection = new CkSocket();
    connection.put_RequireSslCertVerify(true);
    success = connection.Connect(host.getAsString(),443,true,10000);
    if (success != true) {
        System.out.println(connection.lastErrorText());
        return;
        }

    success = rest.UseConnection(connection,false);
    if (success != true) {
        System.out.println(rest.lastErrorText());
        return;
        }

    String responseText = rest.fullRequestNoBody("GET",path.getAsString());
    if (rest.get_LastMethodSuccess() != true) {
        System.out.println(rest.lastErrorText());
        return;
        }

    if (rest.get_ResponseStatusCode() == 404) {
        System.out.println("No account suppression entry for this address in this Region.");
        return;
        }

    if (rest.get_ResponseStatusCode() != 200) {
        System.out.println("SES returned HTTP " + rest.get_ResponseStatusCode() + " " + responseText);
        return;
        }

    CkJsonObject response = new CkJsonObject();
    success = response.Load(responseText);
    if (success != true) {
        System.out.println(response.lastErrorText());
        return;
        }

    System.out.println(response.stringOf("SuppressedDestination.EmailAddress"));
    System.out.println("Reason: " + response.stringOf("SuppressedDestination.Reason"));
    System.out.println("Last updated (Unix seconds): " + response.stringOf("SuppressedDestination.LastUpdateTime"));
  }
}