Dart Requires Chilkat v11.0.0+
Dart
IMAP Read Encrypted Email
See more IMAP Examples
Demonstrates how to read encrypted email from an IMAP mailbox.Reading encrypted email works the same as reading non-encrypted email. If the required certificate and private key are available on the system (e.g., in the macOS Keychain or Windows Certificate Store), Chilkat will automatically locate them, decrypt the email, and handle the process seamlessly.
Information about the original encrypted state of the email is available after it has been downloaded and decrypted.
Chilkat Dart Downloads
import 'package:chilkat/chilkat.dart';
void main() {
final imap = CkImap();
imap.ssl = true;
imap.port = 993;
try {
imap.connect('imap.example2.com');
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
// We'll get the IMAP email account's password from the Apple Keychain or Windows Credentials Manager.
// See how we originally saved the email credentials to the Keychain here:
// Save Email Credentials in Apple Keychain or Windows Credentials Manager
final secrets = CkSecrets();
// On Windows, this is the Windows Credentials Manager
// On MacOS/iOS, it is the Apple Keychain
secrets.location = 'local_manager';
// Specify the name of the secret.
// service and username are required.
// appName and domain are optional.
// Note: The values are arbitrary and can be anything you want.
final json = CkJsonObject();
json.updateString('appName', 'MyEmailApp');
json.updateString('service', 'IMAP');
json.updateString('domain', 'example2.com');
json.updateString('username', 'jane@example2.com');
final String password;
try {
password = secrets.getSecretStr(json);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
try {
imap.login('jane@example2.com', password);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
// Select an IMAP mailbox
try {
imap.selectMailbox('Inbox');
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
// This example: Send Encrypted Email using Certificate in Apple Keychain
// sent an email with the subject "This email is encrypted".
// Let's download an email with the word "encrypted" in the subject.
final messageSet = CkMessageSet();
try {
imap.queryMbx('SUBJECT encrypted', true, messageSet);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
if (messageSet.count == 0) {
print('No messages found.');
return;
}
// Reading encrypted email works the same as reading non-encrypted email.
// If the required certificate and private key are available on the system (e.g., in the macOS Keychain or Windows Certificate Store),
// Chilkat will automatically locate them, decrypt the email, and handle the process seamlessly.
final uid = messageSet.getId(0);
final email = CkEmail();
try {
imap.fetchEmail(false, uid, true, email);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
// Here we can show if the email was received encrypted, if it was successfully decrypted, and
// which certificate was used to decrypt.
print('Email received encrypted: ${email.receivedEncrypted}');
// Was it successfully decrypted?
print('Successfully decrypted: ${email.decrypted}');
// What cert was used to decrypt?
print('Encrypted by: ${email.encryptedBy}');
final cert = CkCert();
try {
email.lastDecryptCert(cert);
print('Certificate DN: ${cert.subjectDN}');
} on ChilkatException {
// email.lastDecryptCert() failed; continue anyway.
}
// Show the decrypted email body.
print(email.body);
imap.disconnect();
}