Sample code for 30+ languages & platforms
React Native Requires Chilkat v11.0.0+

Subscribe to Mailboxes and List Subscribed Mailboxes

See more IMAP Examples

Demonstrates how to list subscribed mailboxes, and unsubscribe/subscribe to a mailbox.

Chilkat React Native Downloads

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

async function chilkatExample() {
  // This example assumes the Chilkat API to have been previously unlocked.
  // See Global Unlock Sample for sample code.

  const imap = new Imap();

  // Connect to an IMAP server.
  // Use TLS
  imap.ssl = true;
  imap.port = 993;
  try {
    await imap.connectAsync('imap.example.com');
  } catch {
    console.log(imap.lastErrorText);
    return;
  }

  // Login
  try {
    await imap.loginAsync('myLogin', 'myPassword');
  } catch {
    console.log(imap.lastErrorText);
    return;
  }

  // First examine the already subscribed mailboxes.
  const refName = '';
  const wildcardedMailbox = '*';
  const subscribed = true;

  const mboxes = new Mailboxes();
  try {
    await imap.mbxListAsync(subscribed, refName, wildcardedMailbox, mboxes);
  } catch {
    console.log(imap.lastErrorText);
    return;
  }

  let i = 0;
  while (i < mboxes.count) {
    console.log(mboxes.getName(i));
    i++;
  }

  console.log('----');

  // Sample output.

  // INBOX
  // INBOX/recent
  // INBOX/misc
  // INBOX/misc/solutions
  // ...
  // Outbox
  // Deleted Items
  // Sent Items
  // Drafts
  // Junk E-mail
  // Trash
  // Sent
  // Templates

  // Let's unsubscribe to "INBOX/misc/solutions".
  try {
    await imap.unsubscribeAsync('INBOX/misc/solutions');
  } catch {
    console.log(imap.lastErrorText);
    return;
  }

  // Get the list of subscribed mailboxes again to verify that "INBOX/misc/solutions" is missing.
  try {
    await imap.mbxListAsync(subscribed, '', 'INBOX/misc*', mboxes);
  } catch {
    console.log(imap.lastErrorText);
    return;
  }

  i = 0;
  while (i < mboxes.count) {
    console.log(mboxes.getName(i));
    i++;
  }

  console.log('----');

  // Re-subscribe to "INBOX/misc/solutions".
  try {
    await imap.subscribeAsync('INBOX/misc/solutions');
  } catch {
    console.log(imap.lastErrorText);
    return;
  }

  // Get the list of subscribed mailboxes again to verify that "INBOX/misc/solutions" is back in the list.
  try {
    await imap.mbxListAsync(subscribed, '', 'INBOX/misc*', mboxes);
  } catch {
    console.log(imap.lastErrorText);
    return;
  }

  i = 0;
  while (i < mboxes.count) {
    console.log(mboxes.getName(i));
    i++;
  }

  console.log('----');

  // Disconnect from the IMAP server.
  await imap.disconnectAsync();
}