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

Get IMAP UID from Email Header

See more IMAP Examples

Demonstrates how to get the IMAP UID from an email header. After fetching an email or header using IMAP, the UID is contained in the ckx-imap-uid header field.

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('login', 'password');
  } catch {
    console.log(imap.lastErrorText);
    return;
  }

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

  const fetchUids = true;
  // Get the message UIDs of all the emails in the mailbox

  const messageSet = new MessageSet();
  try {
    await imap.queryMbxAsync('ALL', fetchUids, messageSet);
  } catch {
    console.log(imap.lastErrorText);
    return;
  }

  const bundle = new EmailBundle();
  const headersOnly = true;
  try {
    await imap.fetchMsgSetAsync(headersOnly, messageSet, bundle);
  } catch {
    console.log(imap.lastErrorText);
    return;
  }

  // The UID of each fetched email header is available 
  // in the ckx-imap-uid header field.
  const email = new Email();
  const msgSet1 = new MessageSet();
  let i = 0;
  const szBundle = bundle.messageCount;
  while (i < szBundle) {
    bundle.emailAt(i, email);

    // Build a message set containing one UID
    const uidStr = email.getHeaderField('ckx-imap-uid');
    msgSet1.fromCompactString(uidStr);

    // Move this message to some other folder.
    try {
      await imap.moveMessagesAsync(msgSet1, 'someOtherFolder');
    } catch {
      console.log(imap.lastErrorText);
      // Exit the loop...
      i = szBundle;
    }

    i++;
  }

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