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

Fetch Single Email by UID or Sequence Number

Assuming the UID is known, download a single email by UID from an IMAP mail server.
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;

//  This example requires the Chilkat API to have been previously unlocked.
//  See Global Unlock Sample for sample code.

var imap = new CkImap();

//  Connect to an IMAP server.
//  Use TLS
imap.Ssl = true;
imap.Port = 993;
success = imap.Connect("imap.example.com");
if (success == false) {
    console.log(imap.LastErrorText);
    return;
}

//  Login
success = imap.Login("***","***");
if (success == false) {
    console.log(imap.LastErrorText);
    return;
}

//  Select an IMAP mailbox
success = imap.SelectMailbox("Inbox");
if (success == false) {
    console.log(imap.LastErrorText);
    return;
}

var email = new CkEmail();

var uid = 2014;
var isUid = true;

success = imap.FetchEmail(false,uid,isUid,email);
if (success == false) {
    console.log(imap.LastErrorText);
    return;
}

//  Display the From and Subject
console.log(email.FromAddress);
console.log(email.Subject);

//  Display the Body property, which is the default body.
//  If an email has an HTML body, the Body property contains
//  the HTML source.  Otherwise it contains the plain-text
//  body.
console.log("---- EMAIL BODY ----");
console.log(email.Body);
console.log("--------------------");

//  Display the recipients:
var j;
for (j = 0; j <= email.NumTo - 1; j++) {
    console.log(email.GetToName(j) + ", " + email.GetToAddr(j));
}

for (j = 0; j <= email.NumCC - 1; j++) {
    console.log(email.GetCcName(j) + ", " + email.GetCcAddr(j));
}

//  Show the total size of the email, including body and attachments:
console.log(email.Size);

//  When a full email is downloaded, we would use the
//  email.NumAttachments property in conjunction with the
//  email.GetMailAttach* methods.
//  However, when an email object contains only the header,
//  we need to access the attachment info differently:
var numAttach = imap.GetMailNumAttach(email);
console.log(numAttach);

for (j = 0; j <= numAttach - 1; j++) {
    console.log(imap.GetMailAttachFilename(email,j));
    var attachSize = imap.GetMailAttachSize(email,j);
    console.log("    size = " + attachSize + " bytes");
}

console.log("--");

//  Disconnect from the IMAP server.
success = imap.Disconnect();