Sample code for 30+ languages & platforms
React Native

Verify DKIM-Signature Headers in Downloaded Email

See more DKIM / DomainKey Examples

Downloads email from an IMAP server and verifies the DKIM-Signature header(s) in each email, if present.

Chilkat React Native Downloads

React Native
import { BinData, Dkim, Imap, JsonObject } from '@chilkat/react-native'

async function chilkatExample() {
  let success = false;

  // 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, login, select mailbox..
  // Use TLS 
  imap.ssl = true;
  imap.port = 993;
  success = true;
  try {
    await imap.connectAsync('imap.example.com');
  } catch {
    success = false;
  }
  if (success) {
    success = true;
    try {
      await imap.loginAsync('myLogin', 'myPassword');
    } catch {
      success = false;
    }
    if (success) {
      success = true;
      try {
        await imap.selectMailboxAsync('Inbox');
      } catch {
        success = false;
      }
    }
  }

  if (!success) {
    console.log(imap.lastErrorText);
    return;
  }

  const dkim = new Dkim();

  // Download a max of 10 emails and verify any DKIM-Signature headers
  // that are present.

  // Download emails by sequence numbers (not UIDs).
  const bUid = false;
  let seqNum = 0;
  let j = 0;
  let n = imap.numMessages;
  if (n > 10) {
    n = 10;
  }

  const json = new JsonObject();
  json.emitCompact = false;

  // To verify DKIM-Signature headers, we need the exact unmodified MIME bytes of each email.
  const mimeData = new BinData();
  seqNum = 1;
  while (seqNum <= n) {
    // The FetchSingleBd method was introduced in v9.5.0.76
    success = true;
    try {
      await imap.fetchSingleBdAsync(seqNum, bUid, mimeData);
    } catch {
      success = false;
    }
    if (!success) {
      console.log(imap.lastErrorText);
      return;
    }

    // Get the number of DKIM-Signature headers.
    const numDkim = dkim.numDkimSigs(mimeData);

    // Verify each..
    j = 0;
    while (j < numDkim) {
      console.log(`------ DKIM Signature ${j}`);

      success = true;
      try {
        dkim.dkimVerify(j, mimeData);
      } catch {
        success = false;
      }
      if (!success) {
        console.log('Not valid.');
      } else {
        console.log('valid.');
      }

      // Show the additional information about the signature verification
      json.load(dkim.verifyInfo);
      console.log(json.emit());

      // The JSON contains information such as this:

      // 	{
      // 	  "domain": "amazonses.com",
      // 	  "selector": "7v7vs6w47njt4pimodk5mmttbegzsi6n",
      // 	  "publicKey": "MIGfMA0GCSqG...v2GvWPqGHz6uqeQIDAQAB",
      // 	  "canonicalization": "relaxed/simple",
      // 	  "algorithm": "rsa-sha256",
      // 	  "signedHeaders": "Subject:From:To:Date:Mime-Version:Content-Type:References:Message-Id:Feedback-ID",
      // 	  "verified": "yes"
      // 	}

      j++;
    }

    seqNum++;
  }

  success = true;
  try {
    await imap.disconnectAsync();
  } catch {
    success = false;
  }
}