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

List IMAP Mailboxes with Reference

Demonstrates how to list all sub-mailboxes within a specified context. In this case, we list all mailboxes under "INBOX.vendors".

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('admin@chilkatsoft.com', 'myPassword');
  } catch {
    console.log(imap.lastErrorText);
    return;
  }

  // Get the list of mailboxes.
  const refName = '';
  const wildcardedMailbox = '*';
  const subscribed = false;

  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++;
  }

  // Sample output looks like this:
  // INBOX.vendors.shareit
  // INBOX.vendors.paypal
  // INBOX.vendors.dell
  // INBOX.vendors.inMotion
  // INBOX.vendors.myhosting
  // INBOX.vendors.peer1

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