Sample code for 30+ languages & platforms
Dart

Find Certificate for Email Encryption

Demonstrates finding the recipient's certificate in the Windows certificate store and using it to send encrypted 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 SMTP server.
  mailman.smtpHost = 'smtp.example.com';

  // Create a new email object
  final email = CkEmail();

  email.subject = 'This email is encrypted';
  email.body = 'This is a digitally encrypted mail';
  email.from = 'Joe <joe@example.com>';
  // Emails are encrypted using the recipient's certificate.
  final recipientEmailAddr = 'jane@example2.com';
  email.addTo('Jane', recipientEmailAddr);

  // Indicate that the email is to be sent encrypted.
  email.sendEncrypted = true;

  // This example demonstrates finding the email encryption certificate
  // on a Windows system where the certificate is stored in the Windows 
  // certificate store.

  final cert = CkCert();
  // The recipient's certificate is used to encrypt.
  // (Because the recipient is the only one in possession of the private key to decrypt.)
  try {
    cert.loadByEmailAddress(recipientEmailAddr);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  // Specify the certificate to be used for encryption.
  email.setEncryptCert(cert);

  try {
    mailman.sendEmail(email);
    print('Encrypted Mail Sent!');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
  }
}