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

Fetch 1st N Headers of Search Results

Calls Search to get a message set, then downloads the 1st N messages in the message set. There are two equivalent ways of doing it: (1) iterate from 0 to N-1 and download each message individually, or (2) create a new message set that contains the 1st N messages and pass it to FetchHeaders. Both are demonstrated here.

Chilkat React Native Downloads

React Native
import { Email, 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('****', '****');
  } catch {
    console.log(imap.lastErrorText);
    return;
  }

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

  // Get the message IDs of all the emails in the mailbox
  // We can choose to fetch UIDs or sequence numbers.
  const fetchUids = true;
  const messageSet = new MessageSet();
  try {
    await imap.queryMbxAsync('ALL', fetchUids, messageSet);
  } catch {
    console.log(imap.lastErrorText);
    return;
  }

  const numFound = messageSet.count;
  if (numFound === 0) {
    console.log('No messages found.');
    return;
  }

  // Get the 1st 10 messages in messageSet.
  let upperBound = 10;
  if (numFound < upperBound) {
    upperBound = numFound;
  }

  let i = 0;
  const bUid = messageSet.hasUids;
  const headerOnly = true;
  const email = new Email();

  while (i < upperBound) {

    try {
      await imap.fetchEmailAsync(headerOnly, messageSet.getId(i), bUid, email);
    } catch {
      console.log(imap.lastErrorText);
      return;
    }

    console.log(`${i}: ${email.subject}`);

    i++;
  }

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