Sample code for 30+ languages & platforms
React Native

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 React Native Downloads

React Native
import { Imap } from '@chilkat/react-native'

async function chilkatExample() {
  // Demonstrates the Imap.Disconnect method, which closes the connection to the IMAP server.

  const imap = new Imap();

  imap.ssl = true;
  imap.port = 993;

  try {
    await imap.connectAsync('imap.example.com');
  } catch {
    console.log(imap.lastErrorText);
    return;
  }

  try {
    await imap.loginAsync('user@example.com', 'myPassword');
  } catch {
    console.log(imap.lastErrorText);
    return;
  }

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

  // Close the connection to the IMAP server.
  try {
    await imap.disconnectAsync();
  } catch {
    console.log(imap.lastErrorText);
    return;
  }

  console.log('Disconnected from the IMAP server.');
}