Sample code for 30+ languages & platforms
Zig Requires Chilkat v11.0.0+

POP3 Verify Signed (S/MIME) Email

Demonstrates how to download and verify digitally signed S/MIME email.

Chilkat Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    const mailman = try chilkat.MailMan.init();
    defer mailman.deinit();

    // Set the POP3 server's hostname
    mailman.setMailHost("pop.example.com");

    // Set the POP3 login/password.
    mailman.setPopUsername("myLogin");
    mailman.setPopPassword("myPassword");

    const st_uidls = try chilkat.StringTable.init();
    defer st_uidls.deinit();
    mailman.fetchUidls(st_uidls) catch {
        std.debug.print("{s}\n", .{try mailman.getLastErrorText(alloc)});
        return;
    };

    const email = try chilkat.Email.init();
    defer email.deinit();
    const cert = try chilkat.Cert.init();
    defer cert.deinit();

    const count = st_uidls.getCount();
    var i: i32 = 0;
    while (i < count) {
        // Download the full email.
        mailman.fetchByUidl(try st_uidls.stringAt(alloc, i), false, 0, email) catch {
            std.debug.print("{s}\n", .{try mailman.getLastErrorText(alloc)});
            return;
        };

        std.debug.print("{d}\n", .{i});
        std.debug.print("From: {s}\n", .{try email.getFrom(alloc)});
        std.debug.print("Subject: {s}\n", .{try email.getSubject(alloc)});

        // 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)});

                email.lastSignerCert(0, cert) catch {
                    std.debug.print("{s}\n", .{try email.getLastErrorText(alloc)});
                    return;
                };

                std.debug.print("Signing cert: {s}\n", .{try cert.getSubjectCN(alloc)});
            }
        } else {
            std.debug.print("Digital signature verification failed.\n", .{});
        }

        i = i + 1;
    }

    mailman.pop3EndSession() catch {};
}