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

IMAP Download and Verify Signed MIME

See more IMAP Examples

Downloads the original MIME of a digitally signed email and saves the .p7s signature along with other MIME parts. It then imports the email into a Chilkat email object to unwrap the S/MIME and verify the signature, and subsequently saves the attachments if they haven't been saved already.

Chilkat React Native Downloads

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

  // Download the 1st email (as MIME) in the Inbox by sequence number.
  const sbMime = new StringBuilder();
  try {
    await imap.fetchSingleAsMimeSbAsync(1, false, sbMime);
  } catch {
    console.log(imap.lastErrorText);
    return;
  }

  // Load it into a MIME object and check to see if it is signed
  const mime = new Mime();
  mime.loadMimeSb(sbMime);
  let alreadySavedParts = false;
  if (mime.containsSignedParts()) {

    // This will save the .p7s and other parts...
    const st = new StringTable();
    try {
      mime.partsToFiles('c:/temp/qa_output', st);
      const numFiles = st.count;
      let i = 0;
      while (i < numFiles) {
        console.log(`Created: ${st.stringAt(i)}`);
        i++;
      }

      alreadySavedParts = true;
    } catch {
      // mime.partsToFiles() failed; continue anyway.
    }
  }

  // Load the MIME into an Email object.  This unwraps the security layers and verifies signatures.
  const email = new Email();
  email.setFromMimeSb(sbMime);

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

      // The certificate used for signing may be obtained
      // by calling email.LastSignerCert.  Index 0 is the first signer.
      const cert = new Cert();
      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.');
    }
  } else {
    console.log('This email was not signed.');
  }

  if (!alreadySavedParts) {
    email.saveAllAttachments('c:/temp/qa_output');
  }
}