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

Create an HTML preview with embedded images

See more Email Object Examples

Make an independent copy of with-attachments.eml, remove ordinary file attachments from the copy, and export its HTML with related images as data URLs. The original retains its attachments. Use a sanitized viewer when showing untrusted HTML.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Email
oleobject loo_Preview
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("with-attachments.eml")
if li_Success <> 1 then
    Write-Debug loo_Email.LastErrorText
    destroy loo_Email
    return
end if

loo_Preview = create oleobject
li_rc = loo_Preview.ConnectToNewObject("Chilkat.Email")

li_Success = loo_Email.MakeCopy(loo_Preview)
if li_Success <> 1 then
    Write-Debug loo_Email.LastErrorText
    destroy loo_Email
    destroy loo_Preview
    return
end if

//  Keep related items: the HTML still refers to them.
loo_Preview.DropAttachments()
Write-Debug "Files in original: " + string(loo_Email.NumAttachments) + "  Files in preview: " + string(loo_Preview.NumAttachments)
if loo_Preview.HasHtmlBody() <> 1 then
    Write-Debug "This message has no HTML preview."
    destroy loo_Email
    destroy loo_Preview
    return
end if

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

li_Success = loo_Preview.GetHtmlBodySb(1,loo_Html)
if li_Success <> 1 then
    Write-Debug loo_Preview.LastErrorText
    destroy loo_Email
    destroy loo_Preview
    destroy loo_Html
    return
end if

li_Success = loo_Html.WriteFile("preview.html","utf-8",0)
if li_Success <> 1 then
    Write-Debug "Failed: html.WriteFile."
    destroy loo_Email
    destroy loo_Preview
    destroy loo_Html
    return
end if

Write-Debug "Saved preview.html."


destroy loo_Email
destroy loo_Preview
destroy loo_Html