Sample code for 30+ languages & platforms
JavaScript

Check the IMAP Socket Connection

See more IMAP Examples

Demonstrates the Chilkat Imap.CheckConnection method, which checks whether the underlying TCP socket is currently connected to the IMAP server. This performs a low-level socket-state check and does not send an IMAP command. This example connects and checks the socket.

Background: CheckConnection sits between the cached IsConnected and a full protocol round trip: it inspects the actual socket without sending an IMAP command, so it can catch a locally-closed socket that the cached flag would miss. It still cannot detect every half-open connection, though — only a real command like Noop that expects a server reply proves the session is genuinely alive end to end.
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.CheckConnection method, which checks whether the underlying TCP
//  socket is currently connected to the IMAP server.  This is a low-level socket-state check
//  and does not send an IMAP command.

var imap = new CkImap();

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

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

//  Perform a low-level check of the socket connection state.
var stillConnected = imap.CheckConnection();
if (stillConnected == true) {
    console.log("The socket is still connected.");
}
else {
    console.log("The socket is no longer connected.");
}

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