Zig Requires Chilkat v11.0.0+
Zig
IMAP Download and Verify Signed (S/MIME) Email
See more IMAP Examples
Demonstrates how to download and verify digitally signed S/MIME email.Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
var success: bool = false;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
const imap = try chilkat.Imap.init();
defer imap.deinit();
// Connect to an IMAP server.
// Use TLS
imap.setSsl(true);
imap.setPort(993);
success = if (imap.connect("imap.example.com")) true else |_| false;
if (!success) {
std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
return;
}
success = if (imap.login("myLogin", "myPassword")) true else |_| false;
if (!success) {
std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
return;
}
// Select an IMAP mailbox
success = if (imap.selectMailbox("Inbox")) true else |_| false;
if (!success) {
std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
return;
}
// We can choose to fetch UIDs or sequence numbers.
const fetch_uids = true;
// Get the message IDs of all the emails in the mailbox
const message_set = try chilkat.MessageSet.init();
defer message_set.deinit();
success = if (imap.queryMbx("ALL", fetch_uids, message_set)) true else |_| false;
if (!success) {
std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
return;
}
const email = try chilkat.Email.init();
defer email.deinit();
const cert = try chilkat.Cert.init();
defer cert.deinit();
var i: i32 = 0;
while (i < message_set.getCount()) {
const uid = message_set.getId(i);
std.debug.print("uid: {s}\n", .{uid});
success = if (imap.fetchEmail(false, uid, true, email)) true else |_| false;
if (!success) {
std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
return;
}
// 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.getReceivedSigned()) {
std.debug.print("This email was signed.\n", .{});
// Check to see if the signatures were verified.
if (email.getSignaturesValid()) {
std.debug.print("Digital signature(s) verified.\n", .{});
std.debug.print("Signer: {s}\n", .{try email.getSignedBy(alloc)});
// Get the certificate used for signing.
success = if (email.lastSignerCert(0, cert)) true else |_| false;
if (!success) {
std.debug.print("Failed to get signing certificate object.\n", .{});
} else {
std.debug.print("Signing cert: {s}\n", .{try cert.getSubjectCN(alloc)});
}
} else {
std.debug.print("Digital signature verification failed.\n", .{});
}
}
i = i + 1;
}
// Disconnect from the IMAP server.
success = if (imap.disconnect()) true else |_| false;
}