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

Read text and binary MIME parts by content type

See more Email Object Examples

Use the same wildcard and inclusion flags for both counting and retrieval. Request decoded text for textual parts and BinData for binary parts. Use GetAttachedEmail when the goal is to inspect a nested email as a message.

Chilkat Node.js Downloads

Node.js
NODEJS_PRELUDE

function chilkatExample() {

    var success = false;

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

    var email = new chilkat.Email();
    success = email.LoadEml("workshop.eml");
    if (success !== true) {
        console.log(email.LastErrorText);
        return;
    }

    var types = "*/*";
    var inlineOnly = false;
    var excludeAttachments = false;
    var count = email.GetNumPartsOfType(types,inlineOnly,excludeAttachments);
    var mediaType = new chilkat.StringBuilder();
    var i = 0;
    while ((i < count)) {
        mediaType.SetString(email.GetNthContentType(i,types,inlineOnly,excludeAttachments));
        if (mediaType.StartsWith("text/",false) == true) {
            var partText = email.GetNthTextPartOfType(i,types,inlineOnly,excludeAttachments);
            if (email.LastMethodSuccess !== true) {
                console.log(email.LastErrorText);
                return;
            }

            console.log(mediaType.GetAsString() + "  " + partText);
        }
        else {
            var data = new chilkat.BinData();
            success = email.GetNthBinaryPartOfTypeBd(i,types,inlineOnly,excludeAttachments,data);
            if (success !== true) {
                console.log(email.LastErrorText);
                return;
            }

            console.log(mediaType.GetAsString() + "  " + data.NumBytes + " bytes");
        }

        i = i+1;
    }


}

chilkatExample();