Sample code for 30+ languages & platforms
Android™

Disconnect from an IMAP Server

See more IMAP Examples

Demonstrates the Chilkat Imap.Disconnect method, which closes the connection to the IMAP server. A failure indicates the connection could not be closed cleanly; the socket is nevertheless no longer intended for use. This example connects, logs in, and then disconnects.

Background: Disconnect closes the underlying TCP socket. It is lower-level than Logout, which first tells the server to end the IMAP session; the tidy shutdown is Logout followed by Disconnect. Always releasing connections matters because servers cap the number of simultaneous IMAP connections per account, and leaked connections can lock you out until they time out.

Chilkat Android™ Downloads

Android™
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;

import android.app.Activity;
import com.chilkatsoft.*;

import android.widget.TextView;
import android.os.Bundle;

public class SimpleActivity extends Activity {

  private static final String TAG = "Chilkat";

  // Called when the activity is first created.
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    boolean success = false;

    //  Demonstrates the Imap.Disconnect method, which closes the connection to the IMAP server.

    CkImap imap = new CkImap();

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

    success = imap.Connect("imap.example.com");
    if (success == false) {
        Log.i(TAG, imap.lastErrorText());
        return;
        }

    success = imap.Login("user@example.com","myPassword");
    if (success == false) {
        Log.i(TAG, imap.lastErrorText());
        return;
        }

    //  ... perform mailbox operations here ...

    //  Close the connection to the IMAP server.
    success = imap.Disconnect();
    if (success == false) {
        Log.i(TAG, imap.lastErrorText());
        return;
        }

    Log.i(TAG, "Disconnected from the IMAP server.");

  }

  static {
      System.loadLibrary("chilkat");

      // Note: If the incorrect library name is passed to System.loadLibrary,
      // then you will see the following error message at application startup:
      //"The application <your-application-name> has stopped unexpectedly. Please try again."
  }
}