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

Scan for Emails with Attachments and Save Attachments to Files

Scan for emails with attachments and save attachments.

Chilkat React Native Downloads

React Native
import { Email, EmailBundle, Imap, MessageSet } from '@chilkat/react-native'

async function chilkatExample() {
  // This example requires 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;
  }

  // We can choose to fetch UIDs or sequence numbers.
  const fetchUids = true;

  // Get the message IDs of all the emails in the mailbox
  const messageSet = new MessageSet();
  try {
    await imap.queryMbxAsync('ALL', fetchUids, messageSet);
  } catch {
    console.log(imap.lastErrorText);
    return;
  }

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

  // Scan for emails with attachments, and save the attachments
  // to a sub-directory.
  const fullEmail = new Email();
  const emailHeader = new Email();
  let i = 0;
  while (i < bundle.messageCount) {
    // The bundle contains email headers..
    bundle.emailAt(i, emailHeader);

    // Does this email have attachments?
    // Use GetMailNumAttach because the attachments
    // are not actually in the email object because
    // we only downloaded headers.
    const numAttach = imap.getMailNumAttach(emailHeader);

    if (numAttach > 0) {
      // Download the entire email and save the
      // attachments. (Remember, we 
      // need to download the entire email because
      // only the headers were previously downloaded.

      // The ckx-imap-uid header field is added when
      // headers are downloaded.  This makes it possible
      // to get the UID from the email object.
      const uidStr = emailHeader.getHeaderField('ckx-imap-uid');
      const uid = parseInt(uidStr, 10);

      try {
        await imap.fetchEmailAsync(false, uid, true, fullEmail);
      } catch {
        console.log(imap.lastErrorText);
        return;
      }

      fullEmail.saveAllAttachments('attachmentsDir');

      for (let j = 0; j < numAttach; j++) {
        const filename = imap.getMailAttachFilename(emailHeader, j);
        console.log(filename);
      }
    }

    i++;
  }

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