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

POP3 Verify Signed (S/MIME) Email

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 mailman = CkMailMan();

  // Set the POP3 server's hostname
  mailman.mailHost = 'pop.example.com';

  // Set the POP3 login/password.
  mailman.popUsername = 'myLogin';
  mailman.popPassword = 'myPassword';

  final stUidls = CkStringTable();
  try {
    mailman.fetchUidls(stUidls);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

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

  final count = stUidls.count;
  var i = 0;
  while (i < count) {
    // Download the full email.
    try {
      mailman.fetchByUidl(stUidls.stringAt(i), false, 0, email);
    } on ChilkatException catch (e) {
      print(e.lastErrorText);
      return;
    }

    print(i);
    print('From: ${email.from}');
    print('Subject: ${email.subject}');

    // 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}');

        try {
          email.lastSignerCert(0, cert);
        } on ChilkatException catch (e) {
          print(e.lastErrorText);
          return;
        }

        print('Signing cert: ${cert.subjectCN}');
      }
    } else {
      print('Digital signature verification failed.');
    }

    i++;
  }

  mailman.pop3EndSession();
}