Sample code for 30+ languages & platforms
PureBasic

Amazon SES v2: Request verification of a sender email address

See more Amazon SES Examples

Create an email identity in the AWS Region where the application sends mail. Use a mailbox you control and credentials with ses:CreateEmailIdentity. SES emails a verification link that expires after 24 hours. After following it, run Check sender verification status in the same Region.

CreateEmailIdentity · Verify an email identity

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkSocket.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkRest.pb"
IncludeFile "CkAuthAws.pb"

Procedure ChilkatExample()

    success.i = 0

    ;  Assumes Chilkat is unlocked. See Global Unlock Sample.

    success = 0

    ;  Use an address whose inbox you can open. SES sends the verification link there.
    request.i = CkJsonObject::ckCreate()
    If request.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckUpdateString(request,"EmailIdentity","sender+reports@example.com")

    ;  Use the same AWS Region for the identity, endpoint, and Signature Version 4.
    region.s = "us-west-2"
    host.i = CkStringBuilder::ckCreate()
    If host.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkStringBuilder::ckAppend(host,"email.")
    CkStringBuilder::ckAppend(host,region)
    CkStringBuilder::ckAppend(host,".amazonaws.com")

    signer.i = CkAuthAws::ckCreate()
    If signer.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ;  Supply the credentials from your application's credential provider.
    CkAuthAws::setCkAccessKey(signer, "AWS_ACCESS_KEY_ID")
    CkAuthAws::setCkSecretKey(signer, "AWS_SECRET_ACCESS_KEY")
    CkAuthAws::setCkRegion(signer, region)
    CkAuthAws::setCkServiceName(signer, "ses")

    connection.i = CkSocket::ckCreate()
    If connection.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkSocket::setCkRequireSslCertVerify(connection, 1)
    success = CkSocket::ckConnect(connection,CkStringBuilder::ckGetAsString(host),443,1,10000)
    If success <> 1
        Debug CkSocket::ckLastErrorText(connection)
        CkJsonObject::ckDispose(request)
        CkStringBuilder::ckDispose(host)
        CkAuthAws::ckDispose(signer)
        CkSocket::ckDispose(connection)
        ProcedureReturn
    EndIf

    rest.i = CkRest::ckCreate()
    If rest.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkRest::ckUseConnection(rest,connection,0)
    If success <> 1
        Debug CkRest::ckLastErrorText(rest)
        CkJsonObject::ckDispose(request)
        CkStringBuilder::ckDispose(host)
        CkAuthAws::ckDispose(signer)
        CkSocket::ckDispose(connection)
        CkRest::ckDispose(rest)
        ProcedureReturn
    EndIf

    success = CkRest::ckSetAuthAws(rest,signer)
    If success <> 1
        Debug CkRest::ckLastErrorText(rest)
        CkJsonObject::ckDispose(request)
        CkStringBuilder::ckDispose(host)
        CkAuthAws::ckDispose(signer)
        CkSocket::ckDispose(connection)
        CkRest::ckDispose(rest)
        ProcedureReturn
    EndIf

    ;  For temporary AWS credentials, supply the session token from the same provider.
    sessionToken.s = ""
    If sessionToken <> ""
        CkRest::ckAddQueryParam(rest,"X-Amz-Security-Token",sessionToken)
    EndIf

    CkRest::ckAddHeader(rest,"Content-Type","application/json")
    responseText.s = CkRest::ckFullRequestString(rest,"POST","/v2/email/identities",CkJsonObject::ckEmit(request))
    If CkRest::ckLastMethodSuccess(rest) <> 1
        Debug CkRest::ckLastErrorText(rest)
        CkJsonObject::ckDispose(request)
        CkStringBuilder::ckDispose(host)
        CkAuthAws::ckDispose(signer)
        CkSocket::ckDispose(connection)
        CkRest::ckDispose(rest)
        ProcedureReturn
    EndIf

    If CkRest::ckResponseStatusCode(rest) = 409
        Debug "This identity already exists in this Region. Check its verification status."
        CkJsonObject::ckDispose(request)
        CkStringBuilder::ckDispose(host)
        CkAuthAws::ckDispose(signer)
        CkSocket::ckDispose(connection)
        CkRest::ckDispose(rest)
        ProcedureReturn
    EndIf

    If CkRest::ckResponseStatusCode(rest) <> 200
        Debug "SES returned HTTP " + Str(CkRest::ckResponseStatusCode(rest)) + " " + responseText
        CkJsonObject::ckDispose(request)
        CkStringBuilder::ckDispose(host)
        CkAuthAws::ckDispose(signer)
        CkSocket::ckDispose(connection)
        CkRest::ckDispose(rest)
        ProcedureReturn
    EndIf

    result.i = CkJsonObject::ckCreate()
    If result.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkJsonObject::ckLoad(result,responseText)
    If success <> 1
        Debug CkJsonObject::ckLastErrorText(result)
        CkJsonObject::ckDispose(request)
        CkStringBuilder::ckDispose(host)
        CkAuthAws::ckDispose(signer)
        CkSocket::ckDispose(connection)
        CkRest::ckDispose(rest)
        CkJsonObject::ckDispose(result)
        ProcedureReturn
    EndIf

    If CkJsonObject::ckBoolOf(result,"VerifiedForSendingStatus") = 1
        Debug "The identity is already verified for sending."
    Else
        Debug "Open the verification email from AWS, follow its link, then check the identity status."
    EndIf



    CkJsonObject::ckDispose(request)
    CkStringBuilder::ckDispose(host)
    CkAuthAws::ckDispose(signer)
    CkSocket::ckDispose(connection)
    CkRest::ckDispose(rest)
    CkJsonObject::ckDispose(result)


    ProcedureReturn
EndProcedure