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

Download MIME Source of Emails in IMAP Mailbox

Demonstrates how to download the MIME source for emails on an IMAP server.

Chilkat React Native Downloads

React Native
import { Imap, StringBuilder } 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;
  }

  // Select an IMAP mailbox
  try {
    await imap.selectMailboxAsync('Inbox');
  } catch {
    console.log(imap.lastErrorText);
    return;
  }

  // The NumMessages property contains the number of messages in the selected mailbox.
  const numMessages = imap.numMessages;
  if (numMessages === 0) {
    console.log('No messages exist in the Inbox.');
    return;
  }

  const sbMime = new StringBuilder();
  for (let seqNum = 1; seqNum <= numMessages; seqNum++) {
    sbMime.clear();
    try {
      await imap.fetchSingleAsMimeSbAsync(seqNum, false, sbMime);
    } catch {
      console.log(imap.lastErrorText);
      return;
    }

    // The MIME source of the downloaded email is contained in sbMime.
  }

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