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

Fetch 1st N Headers of Search Results

Calls Search to get a message set, then downloads the 1st N messages in the message set. There are two equivalent ways of doing it: (1) iterate from 0 to N-1 and download each message individually, or (2) create a new message set that contains the 1st N messages and pass it to FetchHeaders. Both are demonstrated here.

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("****", "****") 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;
    };

    const num_found = message_set.getCount();
    if (num_found == 0) {
        std.debug.print("No messages found.\n", .{});
        return;
    }

    // Get the 1st 10 messages in messageSet.
    var upper_bound: i32 = 10;
    if (num_found < upper_bound) {
        upper_bound = num_found;
    }

    var i: i32 = 0;
    const b_uid = message_set.getHasUids();
    const header_only = true;
    const email = try chilkat.Email.init();
    defer email.deinit();

    while (i < upper_bound) {
        imap.fetchEmail(header_only, message_set.getId(i), b_uid, email) catch {
            std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
            return;
        };

        std.debug.print("{d}: {s}\n", .{ i, try email.getSubject(alloc) });

        i = i + 1;
    }

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