Sample code for 30+ languages & platforms
PowerBuilder Requires Chilkat v11.0.0+

Add an attachment and an inline image

See more Email Object Examples

Add a file attachment from memory and embed an image in the HTML body using its Content-ID. Start with any email that has an HTML body, or use text-and-html.eml from the preceding example. Save the result as with-attachments.eml.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Email
oleobject loo_Data
oleobject loo_Image
string ls_Cid
oleobject loo_Html

li_Success = 0

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

loo_Email = create oleobject
li_rc = loo_Email.ConnectToNewObject("Chilkat.Email")
if li_rc < 0 then
    destroy loo_Email
    MessageBox("Error","Connecting to COM object failed")
    return
end if
li_Success = loo_Email.LoadEml("text-and-html.eml")
if li_Success <> 1 then
    Write-Debug loo_Email.LastErrorText
    destroy loo_Email
    return
end if

loo_Data = create oleobject
li_rc = loo_Data.ConnectToNewObject("Chilkat.BinData")

li_Success = loo_Data.AppendString("Time,Activity~r~n09:00,Welcome~r~n10:00,Field session~r~n","utf-8")
if li_Success <> 1 then
    Write-Debug "Failed: data.AppendString."
    destroy loo_Email
    destroy loo_Data
    return
end if

li_Success = loo_Email.AddAttachmentBd("agenda.csv",loo_Data,"text/csv")
if li_Success <> 1 then
    Write-Debug loo_Email.LastErrorText
    destroy loo_Email
    destroy loo_Data
    return
end if

//  AddRelatedBd returns the identifier that must appear after cid: in the HTML.
loo_Image = create oleobject
li_rc = loo_Image.ConnectToNewObject("Chilkat.BinData")

li_Success = loo_Image.AppendEncoded("iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAAASUlEQVR4nGMQK/ImBv3//x/CYCBSNQkaIKqJ1YCmGkUDsigu1egakOWwqsaiAaICl2p0P8DV4VKNxdP4VWMPJTyqiY04sZGuAQBhyhp6A/ap2AAAAABJRU5ErkJggg==","base64")
if li_Success <> 1 then
    Write-Debug "Failed: image.AppendEncoded."
    destroy loo_Email
    destroy loo_Data
    destroy loo_Image
    return
end if

ls_Cid = loo_Email.AddRelatedBd("confirmed.png",loo_Image)
if loo_Email.LastMethodSuccess <> 1 then
    Write-Debug loo_Email.LastErrorText
    destroy loo_Email
    destroy loo_Data
    destroy loo_Image
    return
end if

loo_Html = create oleobject
li_rc = loo_Html.ConnectToNewObject("Chilkat.StringBuilder")

loo_Html.Append("<p><img alt='Confirmed' src=~"cid:")
loo_Html.Append(ls_Cid)
loo_Html.Append("~"></p>")
loo_Html.Append(loo_Email.GetHtmlBody())
li_Success = loo_Email.AddHtmlAlternativeBody(loo_Html.GetAsString())
if li_Success <> 1 then
    Write-Debug loo_Email.LastErrorText
    destroy loo_Email
    destroy loo_Data
    destroy loo_Image
    destroy loo_Html
    return
end if

li_Success = loo_Email.SaveEml("with-attachments.eml")
if li_Success <> 1 then
    Write-Debug loo_Email.LastErrorText
    destroy loo_Email
    destroy loo_Data
    destroy loo_Image
    destroy loo_Html
    return
end if

Write-Debug "Saved with-attachments.eml."


destroy loo_Email
destroy loo_Data
destroy loo_Image
destroy loo_Html