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

IMAP STARTTLS (Explicit TLS/SSL)

The StartTls property is set to force the Connect method to automatically convert an connection to TLS/SSL via the STARTTLS IMAP command.

This is also known as "explicit TLS/SSL" as opposed to "implicit TLS/SSL". With implicit TLS/SSL, the IMAP client connects on the well-known IMAP TLS/SSL port 993 and the secure channel is immediately established. With explicit TLS/SSL, the IMAP client connects on the typical non-secure port (143 usually) and the converts the connection via the STARTTLS command.

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

    // Indicate that STARTTLS should be used to convert
    // to a secure TLS/SSL connection:
    imap.setStartTls(true);
    imap.setPort(143);

    // Connect to an IMAP server and convert the connection
    // to TLS/SSL via STARTTLS.
    imap.connect("imap.example.com") catch {
        std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
        return;
    };

    // The remainder of this example is the same as for
    // non-TLS/SSL...

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