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

IMAP Download and Verify Signed (S/MIME) Email

See more IMAP Examples

Demonstrates how to download and verify digitally signed S/MIME email.

Chilkat React Native Downloads

React Native
import { Cert, Email, 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;
  }

  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;
  }

  const email = new Email();
  const cert = new Cert();

  let i = 0;
  while (i < messageSet.count) {

    const uid = messageSet.getId(i);
    console.log(`uid: ${uid}`);

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

    // The security layers of signed and/or encrypted emails
    // are automatically "unwrapped" when loaded into
    // a Chilkat email object.
    // An application only needs to check to see if an email
    // was received signed or encrypted, and then examine
    // the success/failure.  For example:
    if (email.receivedSigned) {

      console.log('This email was signed.');

      // Check to see if the signatures were verified.
      if (email.signaturesValid) {
        console.log('Digital signature(s) verified.');
        console.log(`Signer: ${email.signedBy}`);

        // Get the certificate used for signing.

        try {
          email.lastSignerCert(0, cert);
          console.log(`Signing cert: ${cert.subjectCN}`);
        } catch {
          console.log('Failed to get signing certificate object.');
        }
      } else {
        console.log('Digital signature verification failed.');
      }
    }

    i++;
  }

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