Sample code for 30+ languages & platforms
B4X

Amazon SES v2: Verify a sending domain with Easy DKIM

See more Amazon SES Examples

Use a domain you control. Run once to create the regional identity, publish the returned CNAMEs with your DNS provider, then set checkExisting to true and run again to read the status. The target suffix comes from SES. If your DNS provider appends the domain automatically, enter just the relative record name.

CreateEmailIdentity · GetEmailIdentity · Easy DKIM records

Chilkat B4X Downloads

B4X
Dim success As Boolean = False

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

Dim domainName As String = "example.com"
Dim checkExisting As Boolean = False
'  Use the same AWS Region for the identity, endpoint, and Signature Version 4.
Dim region As String = "us-west-2"
Dim host As ChilkatStringBuilder
host.Initialize
host.Append("email.")
host.Append(region)
host.Append(".amazonaws.com")

Dim signer As ChilkatAuthAws
signer.Initialize
'  Supply the credentials from your application's credential provider.
signer.AccessKey = "AWS_ACCESS_KEY_ID"
signer.SecretKey = "AWS_SECRET_ACCESS_KEY"
signer.Region = region
signer.ServiceName = "ses"

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


Dim rest As ChilkatRest
rest.Initialize("rest")
success = rest.UseConnection(connection, False)
If success <> True Then
    Log(rest.LastErrorText)
    Return
End If

success = rest.SetAuthAws(signer)
If success <> True Then
    Log(rest.LastErrorText)
    Return
End If

'  For temporary AWS credentials, supply the session token from the same provider.
Dim sessionToken As String = ""
If sessionToken <> "" Then
    rest.AddQueryParam("X-Amz-Security-Token", sessionToken)
End If

Dim responseText As String = ""
If checkExisting = True Then
    Dim path As ChilkatStringBuilder
    path.Initialize
    path.Append("/v2/email/identities/")
    path.Append(domainName)
    responseText = rest.FullRequestNoBody("GET", path.GetAsString)
Else
    Dim identity As ChilkatJsonObject
    identity.Initialize
    identity.UpdateString("EmailIdentity", domainName)
    rest.AddHeader("Content-Type", "application/json")
    responseText = rest.FullRequestString("POST", "/v2/email/identities", identity.Emit)
End If

If rest.LastMethodSuccess <> True Then
    Log(rest.LastErrorText)
    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("DKIM status: " & response.StringOf("DkimAttributes.Status"))
Log("Verified for sending: " & response.BoolOf("VerifiedForSendingStatus"))
Dim count As Int = response.SizeOfArray("DkimAttributes.Tokens")
If count < 1 Then
    Log("No Easy DKIM tokens were returned. Check this identity's DKIM configuration.")
    Return
End If

If response.HasMember("DkimAttributes.SigningHostedZone") <> True Then
    Log("Find the CNAME targets for this identity in the SES console.")
    Return
End If

Dim zone As String = response.StringOf("DkimAttributes.SigningHostedZone")
Dim i As Int = 0
Do While (i < count)
    response.I = i
    Dim token As String = response.StringOf("DkimAttributes.Tokens[i]")
    Log(token & "._domainkey." & domainName & " CNAME " & token & "." & zone)
    i = i + 1
Loop