Sample code for 30+ languages & platforms
Zig

List IMAP Mailboxes

See more IMAP Examples

Demonstrates the Chilkat Imap.MbxList method, which lists matching mailboxes and appends them to a Mailboxes object. The first argument selects subscribed-only, the second is a reference name, the third is the mailbox name pattern, and the fourth is the Mailboxes object. This example lists all mailboxes and prints each name. (The object is not cleared on success, so avoid reusing one across calls to prevent duplicates.)

Background: This corresponds to the IMAP LIST command (or LSUB when subscribed-only is requested). The pattern uses IMAP wildcards: * matches across hierarchy levels while % matches within a single level, so Archive/% lists just the direct children of Archive. Each returned mailbox also carries flags you can inspect — for example HasInferiors (has sub-folders) and IsSelectable — which is how a client renders a folder tree.

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

    var success: bool = false;

    // Demonstrates the Imap.MbxList method, which lists matching mailboxes and appends them to a
    // Mailboxes object.  The 1st argument selects subscribed-only, the 2nd is a reference name,
    // the 3rd is the mailbox pattern, and the 4th is the Mailboxes object.

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

    // List all mailboxes.  The first argument false requests all (not subscribed-only); "*" matches any name.
    const mailboxes = try chilkat.Mailboxes.init();
    defer mailboxes.deinit();
    imap.mbxList(false, "", "*", mailboxes) catch {
        std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
        return;
    };

    const n = mailboxes.getCount();
    std.debug.print("Mailboxes: {d}\n", .{n});
    var i: i32 = 0;
    i = 0;
    while (i < n) : (i += 1) {
        std.debug.print("{s}\n", .{try mailboxes.getName(alloc, i)});
    }

    success = ERROR: Property not in the Zig package: Imap.Disconnect
ERROR: Property not found: Imap.Disconnect
imap.ERROR;ERROR: ";" expected
}