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

List Email UIDs

List the UIDs of each email in a mailbox.

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

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

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

    // Connect to an IMAP server.
    // Use TLS
    imap.setSsl(true);
    imap.setPort(993);
    imap.connect("MY-IMAP-DOMAIN") catch {
        std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
        return;
    };

    // Login
    imap.login("MY-IMAP-LOGIN", "MY-IMAP-PASSWORD") 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
    // We can choose to fetch UIDs or sequence numbers.
    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;
    };

    var i: i32 = 0;
    const n = message_set.getCount();
    while (i < n) {
        std.debug.print("{d}\n", .{message_set.getId(i)});
        i = i + 1;
    }

    // Disconnect from the IMAP server.
    try imap.disconnect();
}