Sample code for 30+ languages & platforms
Dart 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 Dart Downloads

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

void main() {
  // This example requires the Chilkat API to have been previously unlocked.
  // See Global Unlock Sample for sample code.

  final imap = CkImap();

  // Connect to an IMAP server.
  // Use TLS
  imap.ssl = true;
  imap.port = 993;
  try {
    imap.connect('imap.example.com');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  try {
    imap.login('myLogin', 'myPassword');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  // Select an IMAP mailbox
  try {
    imap.selectMailbox('Inbox');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  // We can choose to fetch UIDs or sequence numbers.
  final fetchUids = true;

  // Get the message IDs of all the emails in the mailbox
  final messageSet = CkMessageSet();
  try {
    imap.queryMbx('ALL', fetchUids, messageSet);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  final email = CkEmail();
  final cert = CkCert();

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

    final uid = messageSet.getId(i);
    print('uid: $uid');

    try {
      imap.fetchEmail(false, uid, true, email);
    } on ChilkatException catch (e) {
      print(e.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) {

      print('This email was signed.');

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

        // Get the certificate used for signing.

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

    i++;
  }

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