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

Fetch Single Email by UID or Sequence Number

Assuming the UID is known, download a single email by UID from an IMAP mail server.

Chilkat React Native Downloads

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

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

  const email = new Email();

  const uid = 2014;
  const isUid = true;

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

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

  // Display the Body property, which is the default body.
  // If an email has an HTML body, the Body property contains
  // the HTML source.  Otherwise it contains the plain-text
  // body.
  console.log('---- EMAIL BODY ----');
  console.log(email.body);
  console.log('--------------------');

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

  for (let j = 0; j < email.numCC; j++) {
    console.log(`${email.getCcName(j)}, ${email.getCcAddr(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);
  console.log(numAttach);

  for (let j = 0; j < numAttach; j++) {
    console.log(imap.getMailAttachFilename(email, j));
    const attachSize = imap.getMailAttachSize(email, j);
    console.log(`    size = ${attachSize} bytes`);
  }

  console.log('--');

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