Sample code for 30+ languages & platforms
PowerBuilder

Amazon SES v2: Send a raw MIME email with a PDF attachment

See more Amazon SES Examples

Place invoice.pdf in the working directory. Chilkat builds the MIME message and gives the attachment the name invoice-1042.pdf. The v2 SendEmail request carries those bytes in Content.Raw.Data. For a blind copy, supply the address in Destination.BccAddresses and keep it out of the MIME headers.

Use a verified sender and grant ses:SendEmail for this v2 operation. The v2 message size limit is 40 MB including attachments after MIME encoding. For a straightforward attachment that needs no custom MIME layout, v2 also supports Content.Simple.Attachments.

Sending raw email · SES attachments · Sending quotas

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Pdf
string ls_Sender
string ls_Recipient
oleobject loo_Email
oleobject loo_Mime
oleobject loo_Request
string ls_Region
oleobject loo_Host
oleobject loo_Signer
oleobject loo_Connection
oleobject loo_Rest
string ls_SessionToken
string ls_ResponseText
oleobject loo_Result

li_Success = 0

//  Assumes Chilkat is unlocked. See Global Unlock Sample.

li_Success = 0

//  Read the PDF before making a network request.
loo_Pdf = create oleobject
li_rc = loo_Pdf.ConnectToNewObject("Chilkat.BinData")
if li_rc < 0 then
    destroy loo_Pdf
    MessageBox("Error","Connecting to COM object failed")
    return
end if
li_Success = loo_Pdf.LoadFile("invoice.pdf")
if li_Success <> 1 then
    Write-Debug loo_Pdf.LastErrorText
    destroy loo_Pdf
    return
end if

if loo_Pdf.NumBytes = 0 then
    Write-Debug "invoice.pdf is empty."
    destroy loo_Pdf
    return
end if

ls_Sender = "sender+reports@example.com"
ls_Recipient = "success@simulator.amazonses.com"
loo_Email = create oleobject
li_rc = loo_Email.ConnectToNewObject("Chilkat.Email")

loo_Email.From = ls_Sender
loo_Email.Subject = "Invoice 1042"
loo_Email.Charset = "utf-8"
li_Success = loo_Email.AddTo("",ls_Recipient)
if li_Success <> 1 then
    Write-Debug loo_Email.LastErrorText
    destroy loo_Pdf
    destroy loo_Email
    return
end if

loo_Email.AddPlainTextAlternativeBody("Invoice 1042 is attached.")
loo_Email.AddHtmlAlternativeBody("<p>Invoice <strong>1042</strong> is attached.</p>")
li_Success = loo_Email.AddAttachmentBd("invoice-1042.pdf",loo_Pdf,"application/pdf")
if li_Success <> 1 then
    Write-Debug loo_Email.LastErrorText
    destroy loo_Pdf
    destroy loo_Email
    return
end if

//  Get the exact MIME bytes, including the attachment's transfer encoding.
loo_Mime = create oleobject
li_rc = loo_Mime.ConnectToNewObject("Chilkat.BinData")

li_Success = loo_Email.GetMimeBd(loo_Mime)
if li_Success <> 1 then
    Write-Debug loo_Email.LastErrorText
    destroy loo_Pdf
    destroy loo_Email
    destroy loo_Mime
    return
end if

//  Base64 here transports the entire MIME message as a blob in the JSON request.
loo_Request = create oleobject
li_rc = loo_Request.ConnectToNewObject("Chilkat.JsonObject")

loo_Request.UpdateString("FromEmailAddress",ls_Sender)
loo_Request.UpdateString("Destination.ToAddresses[0]",ls_Recipient)
loo_Request.UpdateString("Content.Raw.Data",loo_Mime.GetEncoded("base64"))

//  Use the same AWS Region for the identity, endpoint, and Signature Version 4.
ls_Region = "us-west-2"
loo_Host = create oleobject
li_rc = loo_Host.ConnectToNewObject("Chilkat.StringBuilder")

loo_Host.Append("email.")
loo_Host.Append(ls_Region)
loo_Host.Append(".amazonaws.com")

loo_Signer = create oleobject
li_rc = loo_Signer.ConnectToNewObject("Chilkat.AuthAws")

//  Supply the credentials from your application's credential provider.
loo_Signer.AccessKey = "AWS_ACCESS_KEY_ID"
loo_Signer.SecretKey = "AWS_SECRET_ACCESS_KEY"
loo_Signer.Region = ls_Region
loo_Signer.ServiceName = "ses"

loo_Connection = create oleobject
li_rc = loo_Connection.ConnectToNewObject("Chilkat.Socket")

loo_Connection.RequireSslCertVerify = 1
li_Success = loo_Connection.Connect(loo_Host.GetAsString(),443,1,10000)
if li_Success <> 1 then
    Write-Debug loo_Connection.LastErrorText
    destroy loo_Pdf
    destroy loo_Email
    destroy loo_Mime
    destroy loo_Request
    destroy loo_Host
    destroy loo_Signer
    destroy loo_Connection
    return
end if

loo_Rest = create oleobject
li_rc = loo_Rest.ConnectToNewObject("Chilkat.Rest")

li_Success = loo_Rest.UseConnection(loo_Connection,0)
if li_Success <> 1 then
    Write-Debug loo_Rest.LastErrorText
    destroy loo_Pdf
    destroy loo_Email
    destroy loo_Mime
    destroy loo_Request
    destroy loo_Host
    destroy loo_Signer
    destroy loo_Connection
    destroy loo_Rest
    return
end if

li_Success = loo_Rest.SetAuthAws(loo_Signer)
if li_Success <> 1 then
    Write-Debug loo_Rest.LastErrorText
    destroy loo_Pdf
    destroy loo_Email
    destroy loo_Mime
    destroy loo_Request
    destroy loo_Host
    destroy loo_Signer
    destroy loo_Connection
    destroy loo_Rest
    return
end if

//  For temporary AWS credentials, supply the session token from the same provider.
ls_SessionToken = ""
if ls_SessionToken <> "" then
    loo_Rest.AddQueryParam("X-Amz-Security-Token",ls_SessionToken)
end if

loo_Rest.AddHeader("Content-Type","application/json")
ls_ResponseText = loo_Rest.FullRequestString("POST","/v2/email/outbound-emails",loo_Request.Emit())
if loo_Rest.LastMethodSuccess <> 1 then
    Write-Debug loo_Rest.LastErrorText
    destroy loo_Pdf
    destroy loo_Email
    destroy loo_Mime
    destroy loo_Request
    destroy loo_Host
    destroy loo_Signer
    destroy loo_Connection
    destroy loo_Rest
    return
end if

if loo_Rest.ResponseStatusCode <> 200 then
    Write-Debug "SES returned HTTP " + string(loo_Rest.ResponseStatusCode) + " " + ls_ResponseText
    destroy loo_Pdf
    destroy loo_Email
    destroy loo_Mime
    destroy loo_Request
    destroy loo_Host
    destroy loo_Signer
    destroy loo_Connection
    destroy loo_Rest
    return
end if

loo_Result = create oleobject
li_rc = loo_Result.ConnectToNewObject("Chilkat.JsonObject")

li_Success = loo_Result.Load(ls_ResponseText)
if li_Success <> 1 then
    Write-Debug loo_Result.LastErrorText
    destroy loo_Pdf
    destroy loo_Email
    destroy loo_Mime
    destroy loo_Request
    destroy loo_Host
    destroy loo_Signer
    destroy loo_Connection
    destroy loo_Rest
    destroy loo_Result
    return
end if

if loo_Result.HasMember("MessageId") <> 1 then
    Write-Debug "SES returned no message ID."
    destroy loo_Pdf
    destroy loo_Email
    destroy loo_Mime
    destroy loo_Request
    destroy loo_Host
    destroy loo_Signer
    destroy loo_Connection
    destroy loo_Rest
    destroy loo_Result
    return
end if

Write-Debug "SES accepted the message. Message ID: " + loo_Result.StringOf("MessageId")


destroy loo_Pdf
destroy loo_Email
destroy loo_Mime
destroy loo_Request
destroy loo_Host
destroy loo_Signer
destroy loo_Connection
destroy loo_Rest
destroy loo_Result