Sample code for 30+ languages & platforms
Zig

Get the IMAP Server's TLS Certificate

See more IMAP Examples

Demonstrates the Chilkat Imap.GetServerCert method, which retrieves the certificate the IMAP server presented on the current (or most recent) TLS connection. The only argument is a Cert object that receives the certificate. This example prints the subject and issuer common names.

Background: Chilkat already validates the server certificate as part of the TLS handshake, but this method lets an application inspect it directly — to log the issuer, display connection details, implement certificate pinning, or apply an application-specific trust policy. The Cert object exposes the full certificate, including subject, issuer, validity dates, and the public key.

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();

    // Demonstrates the Imap.GetServerCert method, which retrieves the certificate presented by the
    // IMAP server on the current TLS connection.  The only argument is a Cert object that receives
    // the certificate.

    const imap = try chilkat.Imap.init();
    defer imap.deinit();

    imap.setSsl(true);
    imap.setPort(993);

    imap.connect("imap.example.com") catch {
        std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
        return;
    };

    imap.login("user@example.com", "myPassword") catch {
        std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
        return;
    };

    // Get the server's TLS certificate for inspection or diagnostics.
    const cert = try chilkat.Cert.init();
    defer cert.deinit();
    imap.getServerCert(cert) catch {
        std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("Server certificate subject: {s}\n", .{try cert.getSubjectCN(alloc)});
    std.debug.print("Issued by: {s}\n", .{try cert.getIssuerCN(alloc)});

    imap.disconnect() catch {
        std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
        return;
    };
}