Sample code for 30+ languages & platforms
Java

Check the Last Known IMAP Connection State

See more IMAP Examples

Demonstrates the Chilkat Imap.IsConnected method, which returns the last known connection state without sending any data to the IMAP server. A true result does not prove that an idle connection is still usable. This example connects and reads the state.

Background: IsConnected is a cheap, cached check — it reports what Chilkat last observed without touching the network. That makes it fast, but it can be stale: a server or firewall may have silently dropped an idle connection. When you need to be sure the connection is actually alive, send a lightweight round trip such as Noop, or use CheckConnection for a low-level socket probe.

Chilkat Java Downloads

Java
import com.chilkatsoft.*;

public class ChilkatExample {

  static {
    try {
        System.loadLibrary("chilkat");
    } catch (UnsatisfiedLinkError e) {
      System.err.println("Native code library failed to load.\n" + e);
      System.exit(1);
    }
  }

  public static void main(String argv[])
  {
    boolean success = false;

    //  Demonstrates the Imap.IsConnected method, which returns the last known connection state
    //  without sending data to the IMAP server.  A true result does not prove that an idle
    //  connection is still usable.

    CkImap imap = new CkImap();

    imap.put_Ssl(true);
    imap.put_Port(993);

    success = imap.Connect("imap.example.com");
    if (success == false) {
        System.out.println(imap.lastErrorText());
        return;
        }

    //  Check the last known connection state (no data is sent to the server).
    boolean connected = imap.IsConnected();
    if (connected == true) {
        System.out.println("The IMAP object believes it is connected.");
        }
    else {
        System.out.println("The IMAP object is not connected.");
        }

    success = imap.Disconnect();
    if (success == false) {
        System.out.println(imap.lastErrorText());
        return;
        }
  }
}