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

Imap.GetMailSize vs Email.Size

Shows how to get the total size of an email, as well as the sizes of the attachments. This can be done when either full-emails or headers-only are downloaded.

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('****', '****');
  } 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;
  }

  // When downloading headers, each email object contains
  // (obviously) the headers, but the body will be missing.
  // Attachments will not be included.  However, it is
  // possible to get information about the attachments
  // as well as the complete size of the email.
  const bundle = new EmailBundle();
  const headersOnly = true;
  try {
    await imap.fetchMsgSetAsync(headersOnly, messageSet, bundle);
  } catch {
    console.log(imap.lastErrorText);
    return;
  }

  // Loop over the email objects and display information about each:
  const email = new Email();
  let i = 0;
  let j = 0;
  const numEmails = bundle.messageCount;
  while (i < numEmails) {
    bundle.emailAt(i, email);

    // Display the From and Subject
    console.log(email.from);
    console.log(email.subject);

    // Display the recipients:
    j = 0;
    while (j < email.numTo) {
      console.log(`TO: ${email.getToName(j)}, ${email.getToAddr(j)}`);
      j++;
    }

    j = 0;
    while (j < email.numCC) {
      console.log(`CC: ${email.getCcName(j)}, ${email.getCcAddr(j)}`);
      j++;
    }

    // Show the total size of the email, including body and attachments:
    console.log(email.size);

    // When a full email is downloaded, we would use the
    // email.NumAttachments property in conjunction with the
    // email.GetMailAttach* methods.
    // However, when an email object contains only the header,
    // we need to access the attachment info differently:
    const numAttach = imap.getMailNumAttach(email);
    j = 0;
    while (j < numAttach) {
      console.log(imap.getMailAttachFilename(email, j));
      const attachSize = imap.getMailAttachSize(email, j);
      console.log(`    size = ${attachSize} bytes`);
      j++;
    }

    console.log('--');

    i++;
  }

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