Zig Requires Chilkat v11.0.0+
Zig
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 Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
const imap = try chilkat.Imap.init();
defer imap.deinit();
imap.setSsl(true);
imap.setPort(993);
imap.connect("imap.example2.com") catch {
std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
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
const secrets = try chilkat.Secrets.init();
defer secrets.deinit();
// On Windows, this is the Windows Credentials Manager
// On MacOS/iOS, it is the Apple Keychain
secrets.setLocation("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.
const json = try chilkat.JsonObject.init();
defer json.deinit();
json.updateString("appName", "MyEmailApp") catch {};
json.updateString("service", "IMAP") catch {};
json.updateString("domain", "example2.com") catch {};
json.updateString("username", "jane@example2.com") catch {};
const password = secrets.getSecretStr(alloc, json) catch {
std.debug.print("{s}\n", .{try secrets.getLastErrorText(alloc)});
return;
};
imap.login("jane@example2.com", password) catch {
std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
return;
};
// Select an IMAP mailbox
imap.selectMailbox("Inbox") catch {
std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
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.
const message_set = try chilkat.MessageSet.init();
defer message_set.deinit();
imap.queryMbx("SUBJECT encrypted", true, message_set) catch {
std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
return;
};
if (message_set.getCount() == 0) {
std.debug.print("No messages found.\n", .{});
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.
const uid = message_set.getId(0);
const email = try chilkat.Email.init();
defer email.deinit();
imap.fetchEmail(false, uid, true, email) catch {
std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
return;
};
// Here we can show if the email was received encrypted, if it was successfully decrypted, and
// which certificate was used to decrypt.
std.debug.print("Email received encrypted: {}\n", .{email.getReceivedEncrypted()});
// Was it successfully decrypted?
std.debug.print("Successfully decrypted: {}\n", .{email.getDecrypted()});
// What cert was used to decrypt?
std.debug.print("Encrypted by: {s}\n", .{try email.getEncryptedBy(alloc)});
const cert = try chilkat.Cert.init();
defer cert.deinit();
if (email.lastDecryptCert(cert)) {
std.debug.print("Certificate DN: {s}\n", .{try cert.getSubjectDN(alloc)});
} else |_| {}
// Show the decrypted email body.
std.debug.print("{s}\n", .{try email.getBody(alloc)});
imap.disconnect() catch {};
}