Xbase++
Xbase++
Amazon SES v2: Send a plain text email
See more Amazon SES Examples
Send a plain text confirmation using a verified sender and credentials with ses:SendEmail. The SES mailbox simulator also works in the sandbox. Other sandbox recipients must be verified. SES permits up to 50 recipients total across To, CC and BCC. Save the returned message ID to correlate later delivery events.
SendEmail · SES sandbox · Sending quotas
Chilkat Xbase++ Downloads
LOCAL nSuccess
LOCAL oRequest
LOCAL cRegion
LOCAL oHost
LOCAL oSigner
LOCAL oConnection
LOCAL oRest
LOCAL cSessionToken
LOCAL cResponseText
LOCAL oResult
nSuccess := 0
// Assumes Chilkat is unlocked. See Global Unlock Sample.
nSuccess := 0
// Replace the From address with your verified sender. The destination is the SES mailbox simulator.
oRequest := CreateObject("Chilkat.JsonObject")
oRequest:UpdateString("FromEmailAddress", "sender+reports@example.com")
oRequest:UpdateString("Destination.ToAddresses[0]", "success@simulator.amazonses.com")
oRequest:UpdateString("Content.Simple.Subject.Data", "Report ready")
oRequest:UpdateString("Content.Simple.Subject.Charset", "UTF-8")
oRequest:UpdateString("Content.Simple.Body.Text.Data", "Your weekly report is ready to view.")
oRequest:UpdateString("Content.Simple.Body.Text.Charset", "UTF-8")
// Use the same AWS Region for the identity, endpoint, and Signature Version 4.
cRegion := "us-west-2"
oHost := CreateObject("Chilkat.StringBuilder")
oHost:Append("email.")
oHost:Append(cRegion)
oHost:Append(".amazonaws.com")
oSigner := CreateObject("Chilkat.AuthAws")
// Supply the credentials from your application's credential provider.
oSigner:AccessKey := "AWS_ACCESS_KEY_ID"
oSigner:SecretKey := "AWS_SECRET_ACCESS_KEY"
oSigner:Region := cRegion
oSigner:ServiceName := "ses"
oConnection := CreateObject("Chilkat.Socket")
oConnection:RequireSslCertVerify := 1
nSuccess := oConnection:Connect(oHost:GetAsString(), 443, 1, 10000)
IF (nSuccess != 1)
? oConnection:LastErrorText
oRequest:destroy()
oHost:destroy()
oSigner:destroy()
oConnection:destroy()
RETURN
ENDIF
oRest := CreateObject("Chilkat.Rest")
nSuccess := oRest:UseConnection(oConnection, 0)
IF (nSuccess != 1)
? oRest:LastErrorText
oRequest:destroy()
oHost:destroy()
oSigner:destroy()
oConnection:destroy()
oRest:destroy()
RETURN
ENDIF
nSuccess := oRest:SetAuthAws(oSigner)
IF (nSuccess != 1)
? oRest:LastErrorText
oRequest:destroy()
oHost:destroy()
oSigner:destroy()
oConnection:destroy()
oRest:destroy()
RETURN
ENDIF
// For temporary AWS credentials, supply the session token from the same provider.
cSessionToken := ""
IF (cSessionToken != "")
oRest:AddQueryParam("X-Amz-Security-Token", cSessionToken)
ENDIF
oRest:AddHeader("Content-Type", "application/json")
cResponseText := oRest:FullRequestString("POST", "/v2/email/outbound-emails", oRequest:Emit())
IF (oRest:LastMethodSuccess != 1)
? oRest:LastErrorText
oRequest:destroy()
oHost:destroy()
oSigner:destroy()
oConnection:destroy()
oRest:destroy()
RETURN
ENDIF
IF (oRest:ResponseStatusCode != 200)
? "SES returned HTTP " + Str(oRest:ResponseStatusCode) + " " + cResponseText
oRequest:destroy()
oHost:destroy()
oSigner:destroy()
oConnection:destroy()
oRest:destroy()
RETURN
ENDIF
oResult := CreateObject("Chilkat.JsonObject")
nSuccess := oResult:Load(cResponseText)
IF (nSuccess != 1)
? oResult:LastErrorText
oRequest:destroy()
oHost:destroy()
oSigner:destroy()
oConnection:destroy()
oRest:destroy()
oResult:destroy()
RETURN
ENDIF
IF (oResult:HasMember("MessageId") != 1)
? "SES returned no message ID."
oRequest:destroy()
oHost:destroy()
oSigner:destroy()
oConnection:destroy()
oRest:destroy()
oResult:destroy()
RETURN
ENDIF
? "SES accepted the message. Message ID: " + oResult:StringOf("MessageId")
oRequest:destroy()
oHost:destroy()
oSigner:destroy()
oConnection:destroy()
oRest:destroy()
oResult:destroy()