Sample code for 30+ languages & platforms
Java

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

Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Crypt2;
import com.chilkatsoft.chilkat2.DateTime;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.Rest;
import com.chilkatsoft.chilkat2.Socket;
import com.chilkatsoft.chilkat2.StringBuilder;

public class ChilkatExample {

    public static void main(String[] args) {
        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 = "";
        StringBuilder host = new StringBuilder();
        host.append("email.");
        host.append(region);
        host.append(".amazonaws.com");
        StringBuilder mailbox = new StringBuilder();
        mailbox.append(address);
        StringBuilder path = new StringBuilder();
        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.
        StringBuilder canonicalPath = new StringBuilder();
        canonicalPath.append(path.getAsString());
        int replaced = canonicalPath.replace("%", "%25");
        DateTime clock = new DateTime();
        clock.setFromCurrentSystemTime();
        StringBuilder stamp = new StringBuilder();
        stamp.append(clock.getAsTimestamp(false));
        replaced = stamp.replace("-", "");
        replaced = stamp.replace(":", "");
        String date = stamp.getRange(0, 8, false);
        Rest rest = new Rest();
        rest.addHeader("Host", host.getAsString());
        rest.addHeader("x-amz-date", stamp.getAsString());
        StringBuilder headers = new StringBuilder();
        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 (!"".equals(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";
        }

        Crypt2 crypto = new Crypt2();
        crypto.setHashAlgorithm("sha256");
        crypto.setMacAlgorithm("hmac");
        crypto.setEncodingMode("hexlower");
        crypto.setCharset("utf-8");
        StringBuilder canonical = new StringBuilder();
        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(""));
        StringBuilder scope = new StringBuilder();
        scope.append(date);
        scope.append("/");
        scope.append(region);
        scope.append("/ses/aws4_request");
        StringBuilder toSign = new StringBuilder();
        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.
        StringBuilder seed = new StringBuilder();
        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());
        StringBuilder authorization = new StringBuilder();
        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());
        Socket connection = new Socket();
        connection.setRequireSslCertVerify(true);
        String responseText;
        try {
            connection.connect(host.getAsString(), 443, true, 10000);

            rest.useConnection(connection, false);

            responseText = rest.fullRequestNoBody("GET", path.getAsString());
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

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

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

        JsonObject response = new JsonObject();
        try {
            response.load(responseText);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            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"));
    }
}