Sample code for 30+ languages & platforms
Go

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

Go
    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 := chilkat.NewStringBuilder()
    host.Append("email.")
    host.Append(region)
    host.Append(".amazonaws.com")
    mailbox := chilkat.NewStringBuilder()
    mailbox.Append(address)
    path := chilkat.NewStringBuilder()
    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.
    canonicalPath := chilkat.NewStringBuilder()
    canonicalPath.Append(*path.GetAsString())
    replaced := canonicalPath.Replace("%","%25")
    clock := chilkat.NewCkDateTime()
    clock.SetFromCurrentSystemTime()
    stamp := chilkat.NewStringBuilder()
    stamp.Append(*clock.GetAsTimestamp(false))
    replaced = stamp.Replace("-","")
    replaced = stamp.Replace(":","")
    date := stamp.GetRange(0,8,false)
    rest := chilkat.NewRest()
    rest.AddHeader("Host",*host.GetAsString())
    rest.AddHeader("x-amz-date",*stamp.GetAsString())
    headers := chilkat.NewStringBuilder()
    headers.Append("host:")
    headers.Append(*host.GetAsString())
    headers.Append("\nx-amz-date:")
    headers.Append(*stamp.GetAsString())
    headers.Append("\n")
    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"
    }

    crypto := chilkat.NewCrypt2()
    crypto.SetHashAlgorithm("sha256")
    crypto.SetMacAlgorithm("hmac")
    crypto.SetEncodingMode("hexlower")
    crypto.SetCharset("utf-8")
    canonical := chilkat.NewStringBuilder()
    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(""))
    scope := chilkat.NewStringBuilder()
    scope.Append(*date)
    scope.Append("/")
    scope.Append(region)
    scope.Append("/ses/aws4_request")
    toSign := chilkat.NewStringBuilder()
    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.
    seed := chilkat.NewStringBuilder()
    seed.Append("AWS4")
    seed.Append(secretKey)
    crypto.SetMacKeyString(*seed.GetAsString())
    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")
    signature := crypto.MacStringENC(*toSign.GetAsString())
    authorization := chilkat.NewStringBuilder()
    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())
    connection := chilkat.NewSocket()
    connection.SetRequireSslCertVerify(true)
    success = connection.Connect(*host.GetAsString(),443,true,10000)
    if success != true {
        fmt.Println(connection.LastErrorText())
        host.DisposeStringBuilder()
        mailbox.DisposeStringBuilder()
        path.DisposeStringBuilder()
        canonicalPath.DisposeStringBuilder()
        clock.DisposeCkDateTime()
        stamp.DisposeStringBuilder()
        rest.DisposeRest()
        headers.DisposeStringBuilder()
        crypto.DisposeCrypt2()
        canonical.DisposeStringBuilder()
        scope.DisposeStringBuilder()
        toSign.DisposeStringBuilder()
        seed.DisposeStringBuilder()
        authorization.DisposeStringBuilder()
        connection.DisposeSocket()
        return
    }

    success = rest.UseConnection(connection,false)
    if success != true {
        fmt.Println(rest.LastErrorText())
        host.DisposeStringBuilder()
        mailbox.DisposeStringBuilder()
        path.DisposeStringBuilder()
        canonicalPath.DisposeStringBuilder()
        clock.DisposeCkDateTime()
        stamp.DisposeStringBuilder()
        rest.DisposeRest()
        headers.DisposeStringBuilder()
        crypto.DisposeCrypt2()
        canonical.DisposeStringBuilder()
        scope.DisposeStringBuilder()
        toSign.DisposeStringBuilder()
        seed.DisposeStringBuilder()
        authorization.DisposeStringBuilder()
        connection.DisposeSocket()
        return
    }

    responseText := rest.FullRequestNoBody("GET",*path.GetAsString())
    if rest.LastMethodSuccess() != true {
        fmt.Println(rest.LastErrorText())
        host.DisposeStringBuilder()
        mailbox.DisposeStringBuilder()
        path.DisposeStringBuilder()
        canonicalPath.DisposeStringBuilder()
        clock.DisposeCkDateTime()
        stamp.DisposeStringBuilder()
        rest.DisposeRest()
        headers.DisposeStringBuilder()
        crypto.DisposeCrypt2()
        canonical.DisposeStringBuilder()
        scope.DisposeStringBuilder()
        toSign.DisposeStringBuilder()
        seed.DisposeStringBuilder()
        authorization.DisposeStringBuilder()
        connection.DisposeSocket()
        return
    }

    if rest.ResponseStatusCode() == 404 {
        fmt.Println("No account suppression entry for this address in this Region.")
        host.DisposeStringBuilder()
        mailbox.DisposeStringBuilder()
        path.DisposeStringBuilder()
        canonicalPath.DisposeStringBuilder()
        clock.DisposeCkDateTime()
        stamp.DisposeStringBuilder()
        rest.DisposeRest()
        headers.DisposeStringBuilder()
        crypto.DisposeCrypt2()
        canonical.DisposeStringBuilder()
        scope.DisposeStringBuilder()
        toSign.DisposeStringBuilder()
        seed.DisposeStringBuilder()
        authorization.DisposeStringBuilder()
        connection.DisposeSocket()
        return
    }

    if rest.ResponseStatusCode() != 200 {
        fmt.Println("SES returned HTTP ", rest.ResponseStatusCode(), " ", *responseText)
        host.DisposeStringBuilder()
        mailbox.DisposeStringBuilder()
        path.DisposeStringBuilder()
        canonicalPath.DisposeStringBuilder()
        clock.DisposeCkDateTime()
        stamp.DisposeStringBuilder()
        rest.DisposeRest()
        headers.DisposeStringBuilder()
        crypto.DisposeCrypt2()
        canonical.DisposeStringBuilder()
        scope.DisposeStringBuilder()
        toSign.DisposeStringBuilder()
        seed.DisposeStringBuilder()
        authorization.DisposeStringBuilder()
        connection.DisposeSocket()
        return
    }

    response := chilkat.NewJsonObject()
    success = response.Load(*responseText)
    if success != true {
        fmt.Println(response.LastErrorText())
        host.DisposeStringBuilder()
        mailbox.DisposeStringBuilder()
        path.DisposeStringBuilder()
        canonicalPath.DisposeStringBuilder()
        clock.DisposeCkDateTime()
        stamp.DisposeStringBuilder()
        rest.DisposeRest()
        headers.DisposeStringBuilder()
        crypto.DisposeCrypt2()
        canonical.DisposeStringBuilder()
        scope.DisposeStringBuilder()
        toSign.DisposeStringBuilder()
        seed.DisposeStringBuilder()
        authorization.DisposeStringBuilder()
        connection.DisposeSocket()
        response.DisposeJsonObject()
        return
    }

    fmt.Println(*response.StringOf("SuppressedDestination.EmailAddress"))
    fmt.Println("Reason: ", *response.StringOf("SuppressedDestination.Reason"))
    fmt.Println("Last updated (Unix seconds): ", *response.StringOf("SuppressedDestination.LastUpdateTime"))

    host.DisposeStringBuilder()
    mailbox.DisposeStringBuilder()
    path.DisposeStringBuilder()
    canonicalPath.DisposeStringBuilder()
    clock.DisposeCkDateTime()
    stamp.DisposeStringBuilder()
    rest.DisposeRest()
    headers.DisposeStringBuilder()
    crypto.DisposeCrypt2()
    canonical.DisposeStringBuilder()
    scope.DisposeStringBuilder()
    toSign.DisposeStringBuilder()
    seed.DisposeStringBuilder()
    authorization.DisposeStringBuilder()
    connection.DisposeSocket()
    response.DisposeJsonObject()