Sample code for 30+ languages & platforms
React Native

Get the Number of IMAP Attachments

See more IMAP Examples

Demonstrates the Chilkat Imap.GetMailNumAttach method, which returns the number of ordinary attachments on a message. The only argument is the Email. This example fetches only the headers and reports the attachment count.

Background: A header-only fetch downloads no attachment bodies, so the Email object's own downloaded-attachment count can be 0. GetMailNumAttach instead reads the ckx-imap-numAttach metadata, giving the true count — which is what you want when building a message list that shows whether each message has attachments.

Chilkat React Native Downloads

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

async function chilkatExample() {
  // Demonstrates the Imap.GetMailNumAttach method, which returns the number of ordinary
  // attachments on a message.  The only argument is the Email.

  const imap = new Imap();

  imap.ssl = true;
  imap.port = 993;

  try {
    await imap.connectAsync('imap.example.com');
  } catch {
    console.log(imap.lastErrorText);
    return;
  }

  try {
    await imap.loginAsync('user@example.com', 'myPassword');
  } catch {
    console.log(imap.lastErrorText);
    return;
  }

  try {
    await imap.selectMailboxAsync('Inbox');
  } catch {
    console.log(imap.lastErrorText);
    return;
  }

  // Fetch only the message headers.  Attachment bodies are NOT downloaded, but the ckx-imap-*
  // metadata describing the attachments is present, so the attachment info methods still work.
  const headersOnly = true;
  const useUid = false;
  const seqNum = 1;
  const email = new Email();
  try {
    await imap.fetchEmailAsync(headersOnly, seqNum, useUid, email);
  } catch {
    console.log(imap.lastErrorText);
    return;
  }

  const numAttach = imap.getMailNumAttach(email);
  console.log(`This message has ${numAttach} attachment(s).`);

  try {
    await imap.disconnectAsync();
  } catch {
    console.log(imap.lastErrorText);
    return;
  }
}