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

List IMAP Mailboxes with Reference

Demonstrates how to list all sub-mailboxes within a specified context. In this case, we list all mailboxes under "INBOX.vendors".

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

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

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

    // Get the list of mailboxes.
    const ref_name = "";
    const wildcarded_mailbox = "*";
    const subscribed = false;

    const mboxes = try chilkat.Mailboxes.init();
    defer mboxes.deinit();
    imap.mbxList(subscribed, ref_name, wildcarded_mailbox, mboxes) catch {
        std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
        return;
    };

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

    // Sample output looks like this:
    // INBOX.vendors.shareit
    // INBOX.vendors.paypal
    // INBOX.vendors.dell
    // INBOX.vendors.inMotion
    // INBOX.vendors.myhosting
    // INBOX.vendors.peer1

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