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

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 filename = new CkStringBuilder();
var mediaType = new CkStringBuilder();
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 CkBinData();
        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;
}