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

Find and save an attachment and list inline images

See more Email Object Examples

Find an attachment by filename and media type, then save its decoded bytes to a path you choose. Enumerate inline resources separately to read their Content-IDs.

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("with-attachments.eml");
    if (success !== true) {
        console.log(email.LastErrorText);
        return;
    }

    var filename = new chilkat.StringBuilder();
    var mediaType = new chilkat.StringBuilder();
    var found = false;
    var i = 0;
    while ((i < email.NumAttachments)) {
        filename.SetString(email.GetAttachmentFilename(i));
        mediaType.SetString(email.GetAttachmentContentType(i));
        if ((filename.ContentsEqual("agenda.csv",false) == true) && (mediaType.ContentsEqual("text/csv",false) == true) && (found == false)) {
            var data = new chilkat.BinData();
            success = email.GetAttachmentBd(i,data);
            if (success !== true) {
                console.log(email.LastErrorText);
                return;
            }

            success = data.WriteFile("received-agenda.csv");
            if (success !== true) {
                console.log("Failed: data.WriteFile.");
                return;
            }

            console.log("Saved attachment: " + data.NumBytes + " bytes.");
            found = true;
        }

        i = i+1;
    }

    if (found !== true) {
        console.log("No matching attachment.");
    }

    i = 0;
    while ((i < email.NumRelatedItems)) {
        console.log("Related: " + email.GetRelatedContentType(i) + "  Content-ID: " + email.GetRelatedContentID(i));
        i = i+1;
    }


}

chilkatExample();