Sample code for 30+ languages & platforms
Xbase++

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 Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL cAddress
LOCAL cRegion
LOCAL cAccessKey
LOCAL cSecretKey
LOCAL cSessionToken
LOCAL oHost
LOCAL oMailbox
LOCAL oPath
LOCAL oCanonicalPath
LOCAL nReplaced
LOCAL oClock
LOCAL oStamp
LOCAL cDate
LOCAL oRest
LOCAL oHeaders
LOCAL cSignedHeaders
LOCAL oCrypto
LOCAL oCanonical
LOCAL oScope
LOCAL oToSign
LOCAL oSeed
LOCAL cKey
LOCAL cSignature
LOCAL oAuthorization
LOCAL oConnection
LOCAL cResponseText
LOCAL oResponse

nSuccess := 0

//  Assumes Chilkat is unlocked. See Global Unlock Sample.
nSuccess := 0

//  Look up the address exactly as stored, including any plus tag.
cAddress := "avery+orders@example.com"
cRegion := "us-west-2"
cAccessKey := "AWS_ACCESS_KEY_ID"
cSecretKey := "AWS_SECRET_ACCESS_KEY"
cSessionToken := ""
oHost := CreateObject("Chilkat.StringBuilder")
oHost:Append("email.")
oHost:Append(cRegion)
oHost:Append(".amazonaws.com")
oMailbox := CreateObject("Chilkat.StringBuilder")
oMailbox:Append(cAddress)
oPath := CreateObject("Chilkat.StringBuilder")
oPath:Append("/v2/email/suppression/addresses/")
oPath:Append(oMailbox: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.
oCanonicalPath := CreateObject("Chilkat.StringBuilder")
oCanonicalPath:Append(oPath:GetAsString())
nReplaced := oCanonicalPath:Replace("%", "%25")
oClock := CreateObject("Chilkat.CkDateTime")
oClock:SetFromCurrentSystemTime()
oStamp := CreateObject("Chilkat.StringBuilder")
oStamp:Append(oClock:GetAsTimestamp(0))
nReplaced := oStamp:Replace("-", "")
nReplaced := oStamp:Replace(":", "")
cDate := oStamp:GetRange(0, 8, 0)
oRest := CreateObject("Chilkat.Rest")
oRest:AddHeader("Host", oHost:GetAsString())
oRest:AddHeader("x-amz-date", oStamp:GetAsString())
oHeaders := CreateObject("Chilkat.StringBuilder")
oHeaders:Append("host:")
oHeaders:Append(oHost:GetAsString())
oHeaders:Append(Chr(10) + "x-amz-date:")
oHeaders:Append(oStamp:GetAsString())
oHeaders:Append(Chr(10))
cSignedHeaders := "host;x-amz-date"
IF (cSessionToken != "")
    oRest:AddHeader("x-amz-security-token", cSessionToken)
    oHeaders:Append("x-amz-security-token:")
    oHeaders:Append(cSessionToken)
    oHeaders:Append(Chr(10))
    cSignedHeaders := "host;x-amz-date;x-amz-security-token"
ENDIF

oCrypto := CreateObject("Chilkat.Crypt2")
oCrypto:HashAlgorithm := "sha256"
oCrypto:MacAlgorithm := "hmac"
oCrypto:EncodingMode := "hexlower"
oCrypto:Charset := "utf-8"
oCanonical := CreateObject("Chilkat.StringBuilder")
oCanonical:Append("GET" + Chr(10))
oCanonical:Append(oCanonicalPath:GetAsString())
oCanonical:Append(Chr(10) + Chr(10))
oCanonical:Append(oHeaders:GetAsString())
oCanonical:Append(Chr(10))
oCanonical:Append(cSignedHeaders)
oCanonical:Append(Chr(10))
oCanonical:Append(oCrypto:HashStringENC(""))
oScope := CreateObject("Chilkat.StringBuilder")
oScope:Append(cDate)
oScope:Append("/")
oScope:Append(cRegion)
oScope:Append("/ses/aws4_request")
oToSign := CreateObject("Chilkat.StringBuilder")
oToSign:Append("AWS4-HMAC-SHA256" + Chr(10))
oToSign:Append(oStamp:GetAsString())
oToSign:Append(Chr(10))
oToSign:Append(oScope:GetAsString())
oToSign:Append(Chr(10))
oToSign:Append(oCrypto:HashStringENC(oCanonical:GetAsString()))

//  Derive the signing key one HMAC at a time. Decode each intermediate key from hex.
oSeed := CreateObject("Chilkat.StringBuilder")
oSeed:Append("AWS4")
oSeed:Append(cSecretKey)
oCrypto:SetMacKeyString(oSeed:GetAsString())
cKey := oCrypto:MacStringENC(cDate)
oCrypto:SetMacKeyEncoded(cKey, "hex")
cKey := oCrypto:MacStringENC(cRegion)
oCrypto:SetMacKeyEncoded(cKey, "hex")
cKey := oCrypto:MacStringENC("ses")
oCrypto:SetMacKeyEncoded(cKey, "hex")
cKey := oCrypto:MacStringENC("aws4_request")
oCrypto:SetMacKeyEncoded(cKey, "hex")
cSignature := oCrypto:MacStringENC(oToSign:GetAsString())
oAuthorization := CreateObject("Chilkat.StringBuilder")
oAuthorization:Append("AWS4-HMAC-SHA256 Credential=")
oAuthorization:Append(cAccessKey)
oAuthorization:Append("/")
oAuthorization:Append(oScope:GetAsString())
oAuthorization:Append(", SignedHeaders=")
oAuthorization:Append(cSignedHeaders)
oAuthorization:Append(", Signature=")
oAuthorization:Append(cSignature)
oRest:AddHeader("Authorization", oAuthorization:GetAsString())
oConnection := CreateObject("Chilkat.Socket")
oConnection:RequireSslCertVerify := 1
nSuccess := oConnection:Connect(oHost:GetAsString(), 443, 1, 10000)
IF (nSuccess != 1)
    ? oConnection:LastErrorText
    oHost:destroy()
    oMailbox:destroy()
    oPath:destroy()
    oCanonicalPath:destroy()
    oClock:destroy()
    oStamp:destroy()
    oRest:destroy()
    oHeaders:destroy()
    oCrypto:destroy()
    oCanonical:destroy()
    oScope:destroy()
    oToSign:destroy()
    oSeed:destroy()
    oAuthorization:destroy()
    oConnection:destroy()
    RETURN
ENDIF

nSuccess := oRest:UseConnection(oConnection, 0)
IF (nSuccess != 1)
    ? oRest:LastErrorText
    oHost:destroy()
    oMailbox:destroy()
    oPath:destroy()
    oCanonicalPath:destroy()
    oClock:destroy()
    oStamp:destroy()
    oRest:destroy()
    oHeaders:destroy()
    oCrypto:destroy()
    oCanonical:destroy()
    oScope:destroy()
    oToSign:destroy()
    oSeed:destroy()
    oAuthorization:destroy()
    oConnection:destroy()
    RETURN
ENDIF

cResponseText := oRest:FullRequestNoBody("GET", oPath:GetAsString())
IF (oRest:LastMethodSuccess != 1)
    ? oRest:LastErrorText
    oHost:destroy()
    oMailbox:destroy()
    oPath:destroy()
    oCanonicalPath:destroy()
    oClock:destroy()
    oStamp:destroy()
    oRest:destroy()
    oHeaders:destroy()
    oCrypto:destroy()
    oCanonical:destroy()
    oScope:destroy()
    oToSign:destroy()
    oSeed:destroy()
    oAuthorization:destroy()
    oConnection:destroy()
    RETURN
ENDIF

IF (oRest:ResponseStatusCode == 404)
    ? "No account suppression entry for this address in this Region."
    oHost:destroy()
    oMailbox:destroy()
    oPath:destroy()
    oCanonicalPath:destroy()
    oClock:destroy()
    oStamp:destroy()
    oRest:destroy()
    oHeaders:destroy()
    oCrypto:destroy()
    oCanonical:destroy()
    oScope:destroy()
    oToSign:destroy()
    oSeed:destroy()
    oAuthorization:destroy()
    oConnection:destroy()
    RETURN
ENDIF

IF (oRest:ResponseStatusCode != 200)
    ? "SES returned HTTP " + Str(oRest:ResponseStatusCode) + " " + cResponseText
    oHost:destroy()
    oMailbox:destroy()
    oPath:destroy()
    oCanonicalPath:destroy()
    oClock:destroy()
    oStamp:destroy()
    oRest:destroy()
    oHeaders:destroy()
    oCrypto:destroy()
    oCanonical:destroy()
    oScope:destroy()
    oToSign:destroy()
    oSeed:destroy()
    oAuthorization:destroy()
    oConnection:destroy()
    RETURN
ENDIF

oResponse := CreateObject("Chilkat.JsonObject")
nSuccess := oResponse:Load(cResponseText)
IF (nSuccess != 1)
    ? oResponse:LastErrorText
    oHost:destroy()
    oMailbox:destroy()
    oPath:destroy()
    oCanonicalPath:destroy()
    oClock:destroy()
    oStamp:destroy()
    oRest:destroy()
    oHeaders:destroy()
    oCrypto:destroy()
    oCanonical:destroy()
    oScope:destroy()
    oToSign:destroy()
    oSeed:destroy()
    oAuthorization:destroy()
    oConnection:destroy()
    oResponse:destroy()
    RETURN
ENDIF

? oResponse:StringOf("SuppressedDestination.EmailAddress")
? "Reason: " + oResponse:StringOf("SuppressedDestination.Reason")
? "Last updated (Unix seconds): " + oResponse:StringOf("SuppressedDestination.LastUpdateTime")

oHost:destroy()
oMailbox:destroy()
oPath:destroy()
oCanonicalPath:destroy()
oClock:destroy()
oStamp:destroy()
oRest:destroy()
oHeaders:destroy()
oCrypto:destroy()
oCanonical:destroy()
oScope:destroy()
oToSign:destroy()
oSeed:destroy()
oAuthorization:destroy()
oConnection:destroy()
oResponse:destroy()