Sample code for 30+ languages & platforms
Dart

S/MIME Encrypt .eml without Sending

See more Email Object Examples

Demonstrates how to encrypt an email using the recipient's digital certificate. This example just encrypts, and does not send the 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 email = CkEmail();

  try {
    email.loadEml('c:/temp/email/unencrypted.eml');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  // The email content is encrypted using AES with a 256-bit key, operating in GCM mode, which provides authenticated encryption.
  email.pkcs7CryptAlg = 'aes-gcm';
  email.pkcs7KeyLength = 256;
  email.oaepPadding = true;
  email.oaepHash = 'sha256';
  email.oaepMgfHash = 'sha256';

  final cert = CkCert();
  try {
    cert.loadFromFile('c/temps/cert/recipient.cer');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  email.sendEncrypted = true;
  email.setEncryptCert(cert);

  final sbSmime = CkStringBuilder();

  // The mailman object applies the encryption by rendering the email according to the instructions (property settings) provided in the email object.
  // No email is sent.
  final mailman = CkMailMan();
  try {
    mailman.renderToMimeSb(email, sbSmime);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  try {
    sbSmime.writeFile('c:/temp/encryptedEmail.eml', 'utf-8', false);
  } on ChilkatException {
    print(mailman.lastErrorText);
    return;
  }

  print('Success!');
}