Sample code for 30+ languages & platforms
B4X

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

B4X
Dim success As Boolean = False

'  Assumes Chilkat is unlocked. See Global Unlock Sample.
success = False

'  Look up the address exactly as stored, including any plus tag.
Dim address As String = "avery+orders@example.com"
Dim region As String = "us-west-2"
Dim accessKey As String = "AWS_ACCESS_KEY_ID"
Dim secretKey As String = "AWS_SECRET_ACCESS_KEY"
Dim sessionToken As String = ""
Dim host As ChilkatStringBuilder
host.Initialize
host.Append("email.")
host.Append(region)
host.Append(".amazonaws.com")
Dim mailbox As ChilkatStringBuilder
mailbox.Initialize
mailbox.Append(address)
Dim path As ChilkatStringBuilder
path.Initialize
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.
Dim canonicalPath As ChilkatStringBuilder
canonicalPath.Initialize
canonicalPath.Append(path.GetAsString)
Dim replaced As Int = canonicalPath.Replace("%", "%25")
Dim clock As ChilkatDateTime
clock.Initialize
clock.SetFromCurrentSystemTime
Dim stamp As ChilkatStringBuilder
stamp.Initialize
stamp.Append(clock.GetAsTimestamp(False))
replaced = stamp.Replace("-", "")
replaced = stamp.Replace(":", "")
Dim date As String = stamp.GetRange(0, 8, False)
Dim rest As ChilkatRest
rest.Initialize("rest")
rest.AddHeader("Host", host.GetAsString)
rest.AddHeader("x-amz-date", stamp.GetAsString)
Dim headers As ChilkatStringBuilder
headers.Initialize
headers.Append("host:")
headers.Append(host.GetAsString)
headers.Append(Chr(10) & "x-amz-date:")
headers.Append(stamp.GetAsString)
headers.Append(Chr(10))
Dim signedHeaders As String = "host;x-amz-date"
If sessionToken <> "" Then
    rest.AddHeader("x-amz-security-token", sessionToken)
    headers.Append("x-amz-security-token:")
    headers.Append(sessionToken)
    headers.Append(Chr(10))
    signedHeaders = "host;x-amz-date;x-amz-security-token"
End If

Dim crypto As ChilkatCrypt2
crypto.Initialize("crypto")
crypto.HashAlgorithm = "sha256"
crypto.MacAlgorithm = "hmac"
crypto.EncodingMode = "hexlower"
crypto.Charset = "utf-8"
Dim canonical As ChilkatStringBuilder
canonical.Initialize
canonical.Append("GET" & Chr(10))
canonical.Append(canonicalPath.GetAsString)
canonical.Append(Chr(10) & Chr(10))
canonical.Append(headers.GetAsString)
canonical.Append(Chr(10))
canonical.Append(signedHeaders)
canonical.Append(Chr(10))
canonical.Append(crypto.HashStringENC(""))
Dim scope As ChilkatStringBuilder
scope.Initialize
scope.Append(date)
scope.Append("/")
scope.Append(region)
scope.Append("/ses/aws4_request")
Dim toSign As ChilkatStringBuilder
toSign.Initialize
toSign.Append("AWS4-HMAC-SHA256" & Chr(10))
toSign.Append(stamp.GetAsString)
toSign.Append(Chr(10))
toSign.Append(scope.GetAsString)
toSign.Append(Chr(10))
toSign.Append(crypto.HashStringENC(canonical.GetAsString))

'  Derive the signing key one HMAC at a time. Decode each intermediate key from hex.
Dim seed As ChilkatStringBuilder
seed.Initialize
seed.Append("AWS4")
seed.Append(secretKey)
crypto.SetMacKeyString(seed.GetAsString)
Dim key As String = 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")
Dim signature As String = crypto.MacStringENC(toSign.GetAsString)
Dim authorization As ChilkatStringBuilder
authorization.Initialize
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)
Dim connection As ChilkatSocket
connection.Initialize("connection")
connection.RequireSslCertVerify = True
success = connection.Connect(host.GetAsString, 443, True, 10000)
If success <> True Then
    Log(connection.LastErrorText)
    Return
End If

success = rest.UseConnection(connection, False)
If success <> True Then
    Log(rest.LastErrorText)
    Return
End If


Dim responseText As String = rest.FullRequestNoBody("GET", path.GetAsString)
If rest.LastMethodSuccess <> True Then
    Log(rest.LastErrorText)
    Return
End If

If rest.ResponseStatusCode = 404 Then
    Log("No account suppression entry for this address in this Region.")
    Return
End If

If rest.ResponseStatusCode <> 200 Then
    Log("SES returned HTTP " & rest.ResponseStatusCode & " " & responseText)
    Return
End If

Dim response As ChilkatJsonObject
response.Initialize
success = response.Load(responseText)
If success <> True Then
    Log(response.LastErrorText)
    Return
End If

Log(response.StringOf("SuppressedDestination.EmailAddress"))
Log("Reason: " & response.StringOf("SuppressedDestination.Reason"))
Log("Last updated (Unix seconds): " & response.StringOf("SuppressedDestination.LastUpdateTime"))