Sample code for 30+ languages & platforms
B4X

Amazon SES v2: Send private appointment reminders and inspect each result

See more Amazon SES Examples

Replace the two recipients with test mailboxes you control. The example supplies an inline template and one private destination per entry. Store each successful message ID against its appointment. Schedule a retry with backoff only for entries with a retryable outcome. Correct permanent errors before resubmission.

SendBulkEmail · Per-entry outcomes

Chilkat B4X Downloads

B4X
Dim success As Boolean = False

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

Dim request As ChilkatJsonObject
request.Initialize
request.UpdateString("FromEmailAddress", "sender@example.com")
request.UpdateString("DefaultContent.Template.TemplateContent.Subject", "Appointment reminder")
request.UpdateString("DefaultContent.Template.TemplateContent.Text", "Hello {{name}}, your appointment is {{slot}}.")
request.UpdateString("DefaultContent.Template.TemplateData", "{" & Chr(34) & "name" & Chr(34) & ":" & Chr(34) & "there" & Chr(34) & "," & Chr(34) & "slot" & Chr(34) & ":" & Chr(34) & "in your booking confirmation" & Chr(34) & "}")

Dim firstReminder As ChilkatJsonObject
firstReminder.Initialize
firstReminder.UpdateString("name", "Avery")
firstReminder.UpdateString("slot", "Tuesday at 9 AM Central")
request.UpdateString("BulkEmailEntries[0].Destination.ToAddresses[0]", "avery@example.com")
request.UpdateString("BulkEmailEntries[0].ReplacementEmailContent.ReplacementTemplate.ReplacementTemplateData", firstReminder.Emit)

Dim secondReminder As ChilkatJsonObject
secondReminder.Initialize
secondReminder.UpdateString("name", "Robin")
secondReminder.UpdateString("slot", "Wednesday at 2 PM Central")
request.UpdateString("BulkEmailEntries[1].Destination.ToAddresses[0]", "robin@example.com")
request.UpdateString("BulkEmailEntries[1].ReplacementEmailContent.ReplacementTemplate.ReplacementTemplateData", secondReminder.Emit)
'  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


rest.AddHeader("Content-Type", "application/json")
Dim responseText As String = rest.FullRequestString("POST", "/v2/email/outbound-bulk-emails", request.Emit)
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

Dim count As Int = response.SizeOfArray("BulkEmailEntryResults")
If count <> request.SizeOfArray("BulkEmailEntries") Then
    Log("Incomplete results. Reconcile the original batch before retrying.")
    Return
End If

Dim i As Int = 0
Do While (i < count)
    response.I = i
    request.I = i
    Dim recipient As String = request.StringOf("BulkEmailEntries[i].Destination.ToAddresses[0]")
    Dim status As String = response.StringOf("BulkEmailEntryResults[i].Status")
    If status = "SUCCESS" Then
        Log(recipient & " accepted: " & response.StringOf("BulkEmailEntryResults[i].MessageId"))
    Else
        If (status = "ACCOUNT_THROTTLED") Or (status = "TRANSIENT_FAILURE") Then
            Log(recipient & " retry later: " & status)
        Else
            Log(recipient & " review before retry: " & status & " " & response.StringOf("BulkEmailEntryResults[i].Error"))
        End If

    End If

    i = i + 1
Loop