Sample code for 30+ languages & platforms
JavaScript

Fetch the Flags Set on an IMAP Message

See more IMAP Examples

Demonstrates the Chilkat Imap.FetchFlags method, which returns the flags currently set on a single message as a space-separated string. The first argument is the message ID and the second (bUid) selects UID vs sequence number. This example fetches and prints the flags for one message identified by UID.

Background: The returned string is the server's current flag list for the message, such as \Seen \Flagged. This is a quick way to check a single message's state — for example, to decide whether it still needs processing — without downloading the message itself. To read the same information from a message you already have as an Email object, use GetMailFlag instead.
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;

//  Demonstrates the Imap.FetchFlags method, which returns the flags currently set on a single
//  message as a space-separated string.  The 1st argument is the message ID and the 2nd (bUid)
//  selects UID vs sequence number.

var imap = new CkImap();

imap.Ssl = true;
imap.Port = 993;

success = imap.Connect("imap.example.com");
if (success == false) {
    console.log(imap.LastErrorText);
    return;
}

success = imap.Login("user@example.com","myPassword");
if (success == false) {
    console.log(imap.LastErrorText);
    return;
}

success = imap.SelectMailbox("Inbox");
if (success == false) {
    console.log(imap.LastErrorText);
    return;
}

var msgSet = new CkMessageSet();
success = imap.QueryMbx("ALL",true,msgSet);
if (success == false) {
    console.log(imap.LastErrorText);
    return;
}

if (msgSet.Count > 0) {
    var uid = msgSet.GetId(0);
    //  FetchFlags returns a string, so assign it to a string variable and check for success.
    var flags = imap.FetchFlags(uid,true);
    if (imap.LastMethodSuccess == false) {
        console.log(imap.LastErrorText);
        return;
    }

    console.log("UID " + uid + " flags: " + flags);
}

success = imap.Disconnect();
if (success == false) {
    console.log(imap.LastErrorText);
    return;
}