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

Download Unread Email from IMAP Mailbox

Download unread email messages from an IMAP mailbox.

Chilkat React Native Downloads

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

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

  // Find emails marked as seen or not already seen:
  const seenSearch = 'SEEN';
  const notSeenSearch = 'NOT SEEN';

  // Get the set of unseen message UIDs
  const fetchUids = true;
  const messageSet = new MessageSet();
  try {
    await imap.queryMbxAsync(notSeenSearch, fetchUids, messageSet);
  } catch {
    console.log(imap.lastErrorText);
    return;
  }

  // Fetch the unseen emails into a bundle object:
  const bundle = new EmailBundle();
  const headersOnly = false;
  try {
    await imap.fetchMsgSetAsync(headersOnly, messageSet, bundle);
  } catch {
    console.log(imap.lastErrorText);
    return;
  }

  // Display the Subject and From of each email.
  const email = new Email();
  let i = 0;
  const numEmails = bundle.messageCount;
  while (i < numEmails) {
    bundle.emailAt(i, email);

    console.log(email.getHeaderField('Date'));
    console.log(email.subject);
    console.log(email.from);
    console.log('--');
    i++;
  }

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