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

Read email headers and display the body as text

See more Email Object Examples

Read the sender, recipients, subject, date, and an optional custom header from an EML file. Display the plain-text body when available, or convert HTML to text. Handle messages that contain neither body format.

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;
}

console.log("From: " + email.From);
console.log("Subject: " + email.Subject);
console.log("Date: " + email.EmailDateStr);
var i = 0;
while ((i < email.NumTo)) {
    console.log("To: " + email.GetToName(i) + "  " + email.GetToAddr(i));
    i = i+1;
}

var referenceId = email.GetHeaderField("X-Reference-ID");
if (email.LastMethodSuccess == true) {
    console.log("Reference: " + referenceId);
}

if (email.HasPlainTextBody() == true) {
    console.log(email.GetPlainTextBody());
}
else {
    if (email.HasHtmlBody() == true) {
        var reader = new CkHtmlToText();
        console.log(reader.ToText(email.GetHtmlBody()));
    }
    else {
        console.log("No text or HTML body.");
    }

}