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

Note
This example is intended for running within a Chilkat.Js embedded JavaScript engine. All Chilkat JavaScript examples require Chilkat v11.4.0 or greater.
JavaScript
var success = false;

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

var email = new CkEmail();
success = email.LoadEml("with-attachments.eml");
if (success !== true) {
    console.log(email.LastErrorText);
    return;
}

var preview = new CkEmail();
success = email.MakeCopy(preview);
if (success !== true) {
    console.log(email.LastErrorText);
    return;
}

//  Keep related items: the HTML still refers to them.
preview.DropAttachments();
console.log("Files in original: " + email.NumAttachments + "  Files in preview: " + preview.NumAttachments);
if (preview.HasHtmlBody() !== true) {
    console.log("This message has no HTML preview.");
    return;
}

var html = new CkStringBuilder();
success = preview.GetHtmlBodySb(true,html);
if (success !== true) {
    console.log(preview.LastErrorText);
    return;
}

success = html.WriteFile("preview.html","utf-8",false);
if (success !== true) {
    console.log("Failed: html.WriteFile.");
    return;
}

console.log("Saved preview.html.");