Sample code for 30+ languages & platforms
JavaScript

Test for an IMAP Server Capability

See more IMAP Examples

Demonstrates the Chilkat Imap.HasCapability method, which tests whether a named capability appears in a raw capability response. The second argument is typically the string returned by the Capability method. This example checks whether the server supports the IDLE extension.

Background: Rather than scanning the raw CAPABILITY text yourself, HasCapability parses it for a specific token. This is the right guard before using any optional feature — for example, only entering an IDLE wait loop, or using the MOVE command, when the server actually advertises it. Fetch the capability string once and test it as many times as needed.
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.HasCapability method, which tests whether a named capability appears
//  in a raw capability response.  The 2nd argument is typically the string returned by the
//  Capability method.

var imap = new CkImap();

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

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

//  Get the raw capability response.
var capabilities = imap.Capability();
if (imap.LastMethodSuccess == false) {
    console.log(imap.LastErrorText);
    return;
}

//  Test whether the server supports the IDLE extension.
var hasIdle = imap.HasCapability("IDLE",capabilities);
if (hasIdle == true) {
    console.log("The server supports IDLE.");
}
else {
    console.log("The server does not support IDLE.");
}

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