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

Verify each DKIM signature in email downloaded over IMAP

See more DKIM Examples

Download the first ten messages (or all messages in a smaller inbox) as complete MIME, including attachments, while preserving their unread flags. The verifier looks up the selector’s TXT record when it needs a key. Inspect VerifyInfo immediately after each attempt for the signing domain, selector, and any body-hash mismatch.

Verify each signature independently, in top-to-bottom order. A passing signature authenticates the signing domain’s responsibility for the signed content. Identifying the author and checking DMARC alignment with the visible From address are separate steps. A missing DNS key or unavailable resolver can also prevent verification. DKIM specification.

Chilkat Dart Downloads

Dart
import 'package:chilkat/chilkat.dart';

void main() {
  var success = false;

  // Assumes Chilkat is unlocked. See Global Unlock Sample.

  final imap = CkImap();
  imap.ssl = true;
  imap.port = 993;
  imap.requireSslCertVerify = true;
  imap.peekMode = true;
  imap.autoDownloadAttachments = true;
  success = true;
  try {
    imap.connect('imap.example.com');
  } on ChilkatException {
    success = false;
  }
  if (!success) {
    print(imap.lastErrorText);
    return;
  }

  success = true;
  try {
    imap.login('IMAP_USERNAME', 'IMAP_PASSWORD');
  } on ChilkatException {
    success = false;
  }
  if (!success) {
    print(imap.lastErrorText);
    return;
  }

  success = true;
  try {
    imap.selectMailbox('INBOX');
  } on ChilkatException {
    success = false;
  }
  if (!success) {
    print(imap.lastErrorText);
    return;
  }

  // Select the first ten sequence positions, then fetch by stable UID.
  final messages = CkMessageSet();
  success = true;
  try {
    imap.queryMbx('1:10', true, messages);
  } on ChilkatException {
    success = false;
  }
  if (!success) {
    print(imap.lastErrorText);
    return;
  }

  var i = 0;
  final verifier = CkDkim();
  final message = CkBinData();
  while (i < messages.count) {
    final uid = messages.getId(i);
    print('Message UID $uid');
    success = true;
    try {
      imap.fetchSingleBd(uid, true, message);
    } on ChilkatException {
      success = false;
    }
    if (!success) {
      print(imap.lastErrorText);
    } else {
      final count = verifier.numDkimSigs(message);
      if (count == 0) {
        print('No DKIM signatures.');
      }

      var sig = 0;
      while (sig < count) {
        success = true;
        try {
          verifier.dkimVerify(sig, message);
        } on ChilkatException {
          success = false;
        }
        // Read diagnostics before the next verification replaces them.
        final info = CkJsonObject();
        var haveInfo = true;
        try {
          info.load(verifier.verifyInfo);
        } on ChilkatException {
          haveInfo = false;
        }
        if (success) {
          print('Signature $sig verified.');
        } else {
          if ((haveInfo) && (info.stringOfEquals('bodyHashVerified', 'no', true))) {
            print('Signature $sig: the body hash differs.');
          } else {
            print('Signature $sig could not be verified.');
          }

          print(verifier.lastErrorText);
        }

        if ((haveInfo) && (info.jsonTypeOf('domain') == 1)) {
          print('Signing domain: ${info.stringOf('domain')}  Selector: ${info.stringOf('selector')}');
        }

        sig++;
      }
    }

    i++;
  }

  success = true;
  try {
    imap.disconnect();
  } on ChilkatException {
    success = false;
  }
}