Sample code for 30+ languages & platforms
Xbase++ 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 Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oEmail
LOCAL oPreview
LOCAL oHtml

nSuccess := 0

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

oEmail := CreateObject("Chilkat.Email")
nSuccess := oEmail:LoadEml("with-attachments.eml")
IF (nSuccess != 1)
    ? oEmail:LastErrorText
    oEmail:destroy()
    RETURN
ENDIF

oPreview := CreateObject("Chilkat.Email")
nSuccess := oEmail:MakeCopy(oPreview)
IF (nSuccess != 1)
    ? oEmail:LastErrorText
    oEmail:destroy()
    oPreview:destroy()
    RETURN
ENDIF

//  Keep related items: the HTML still refers to them.
oPreview:DropAttachments()
? "Files in original: " + Str(oEmail:NumAttachments) + "  Files in preview: " + Str(oPreview:NumAttachments)
IF (oPreview:HasHtmlBody() != 1)
    ? "This message has no HTML preview."
    oEmail:destroy()
    oPreview:destroy()
    RETURN
ENDIF

oHtml := CreateObject("Chilkat.StringBuilder")
nSuccess := oPreview:GetHtmlBodySb(1, oHtml)
IF (nSuccess != 1)
    ? oPreview:LastErrorText
    oEmail:destroy()
    oPreview:destroy()
    oHtml:destroy()
    RETURN
ENDIF

nSuccess := oHtml:WriteFile("preview.html", "utf-8", 0)
IF (nSuccess != 1)
    ? "Failed: html.WriteFile."
    oEmail:destroy()
    oPreview:destroy()
    oHtml:destroy()
    RETURN
ENDIF

? "Saved preview.html."

oEmail:destroy()
oPreview:destroy()
oHtml:destroy()