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

Save attachments from a received message

See more Email Object Examples

Use a dedicated output directory for this message. The example lists the declared names and sizes, then saves downloaded attachment bodies. A related image in HTML and an attached email have their own collections.

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

if (email.NumAttachments == 0) {
    console.log("There are no ordinary file attachments.");
    return;
}

var i = 0;
while ((i < email.NumAttachments)) {
    console.log(email.GetAttachmentFilename(i) + "  " + email.GetAttachmentSize(i) + " bytes");
    i = i+1;
}

//  Use a fresh output folder per message and keep existing files.
email.OverwriteExisting = false;
success = email.SaveAllAttachments("workshop-attachments");
if (success !== true) {
    console.log(email.LastErrorText);
    return;
}

console.log("Saved attachments in workshop-attachments.");