Zig Requires Chilkat v11.0.0+
Zig
Using Client Certificate w/ IMAP SSL
Demonstrates how to use a client-side certificate with an IMAP SSL connection. The SetSslClientCert method is called to specify a certificate to be used for the SSL connection.Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
const imap = try chilkat.Imap.init();
defer imap.deinit();
// To use a secure SSL connection, set SSL and the port:
imap.setSsl(true);
// The typical port for IMAP SSL is 993
imap.setPort(993);
// Load a certificate from a PFX file and use it.
// Note: Other methods are available to load pre-installed
// certificates from registry-based certificate stores.
// Create an instance of a certificate store object, load a PFX file,
// locate the certificate we need, and use it for signing.
// (a PFX file may contain more than one certificate.)
const cert_store = try chilkat.CertStore.init();
defer cert_store.deinit();
// The 1st argument is the filename, the 2nd arg is the
// PFX file's password:
cert_store.loadPfxFile("myCertWithPrivateKey.pfx", "secret") catch {
std.debug.print("{s}\n", .{try cert_store.getLastErrorText(alloc)});
return;
};
// Find the certificate by the subject common name:
const json_cn = try chilkat.JsonObject.init();
defer json_cn.deinit();
json_cn.updateString("CN", "cert common name") catch {};
const cert = try chilkat.Cert.init();
defer cert.deinit();
cert_store.findCert(json_cn, cert) catch {
std.debug.print("{s}\n", .{try cert_store.getLastErrorText(alloc)});
return;
};
// If a PFX file is known to contain a single certificate,
// you may load it directly into a Chilkat certificate object.
// This snippet of source code shows how:
const cert2 = try chilkat.Cert.init();
defer cert2.deinit();
// The 1st argument is the filename, the 2nd arg is the
// PFX file's password:
cert2.loadPfxFile("myClientCert.pfx", "secret") catch {
std.debug.print("{s}\n", .{try cert.getLastErrorText(alloc)});
return;
};
// Use the cert:
try imap.setSslClientCert(cert);
// Connect to an IMAP server.
imap.connect("imap.example.com") catch {
std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
return;
};
// Login
imap.login("myLogin", "myPassword") 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;
};
// Get the message IDs of all the emails in the mailbox
const fetch_uids = true;
const message_set = try chilkat.MessageSet.init();
defer message_set.deinit();
imap.queryMbx("ALL", fetch_uids, message_set) catch {
std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
return;
};
// Fetch the emails into a bundle object:
const bundle = try chilkat.EmailBundle.init();
defer bundle.deinit();
const headers_only = false;
imap.fetchMsgSet(headers_only, message_set, bundle) catch {
std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
return;
};
// Loop over the bundle and display the FROM and SUBJECT of each.
const email = try chilkat.Email.init();
defer email.deinit();
var i: i32 = 0;
const num_emails = bundle.getMessageCount();
while (i < num_emails) {
bundle.emailAt(i, email) catch {};
std.debug.print("{s}\n", .{try email.getFrom(alloc)});
std.debug.print("{s}\n", .{try email.getSubject(alloc)});
std.debug.print("--\n", .{});
i = i + 1;
}
// Disconnect from the IMAP server.
try imap.disconnect();
}