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

Read IMAP Email Headers

This example demonstrates how to connect to an IMAP server and download only the email headers for all messages in a mailbox. Downloading headers only is much faster and more efficient than downloading complete emails because the message bodies and attachment contents are not retrieved.

The example shows how to:

  • Connect to an IMAP server using SSL/TLS
  • Authenticate and select a mailbox
  • Query for all messages in the mailbox
  • Fetch header-only email information
  • Display sender, subject, recipients, and total message size
  • Retrieve attachment metadata such as filenames and sizes without downloading the actual attachments

This technique is useful for quickly scanning large mailboxes, building message summaries, or inspecting attachment information while minimizing bandwidth and memory usage.

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 has already been unlocked.
    // See Global Unlock Sample for example code.

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

    // Connect to the IMAP server using SSL/TLS on the standard secure IMAP port.
    imap.setSsl(true);
    imap.setPort(993);
    imap.connect("imap.example.com") catch {
        std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
        return;
    };

    // Authenticate with the IMAP server.
    imap.login("****", "****") catch {
        std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
        return;
    };

    // Select the mailbox (folder) to access.
    imap.selectMailbox("Inbox") catch {
        std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
        return;
    };

    // Get the identifiers for all messages in the selected mailbox.
    // Setting fetchUids = cktrue causes IMAP UIDs to be returned.
    // If ckfalse, IMAP sequence numbers are returned instead.
    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;
    };

    // Download only the email headers for the messages in the set.
    // This is much faster than downloading full emails because the
    // message bodies and attachment contents are not retrieved.
    //
    // Even though the attachments themselves are not downloaded,
    // Chilkat can still provide information such as attachment
    // filenames, attachment sizes, and the total message size.
    const bundle = try chilkat.EmailBundle.init();
    defer bundle.deinit();
    const headers_only = true;
    imap.fetchMsgSet(headers_only, message_set, bundle) catch {
        std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
        return;
    };

    // Iterate over each email in the bundle and display summary information.
    const email = try chilkat.Email.init();
    defer email.deinit();
    var i: i32 = 0;
    var j: i32 = 0;
    while (i < bundle.getMessageCount()) {
        bundle.emailAt(i, email) catch {};

        // Display the sender and subject line.
        std.debug.print("{s}\n", .{try email.getFrom(alloc)});
        std.debug.print("{s}\n", .{try email.getSubject(alloc)});

        // Display all "To" recipients.
        j = 0;
        while (j < email.getNumTo()) {
            std.debug.print("TO: {s}, {s}\n", .{ try email.getToName(alloc, j), try email.getToAddr(alloc, j) });
            j = j + 1;
        }

        // Display all "CC" recipients.
        j = 0;
        while (j < email.getNumCC()) {
            std.debug.print("CC: {s}, {s}\n", .{ try email.getCcName(alloc, j), try email.getCcAddr(alloc, j) });
            j = j + 1;
        }

        // Display the total size of the email message, including
        // the body and all attachments, even though only headers
        // were downloaded.
        std.debug.print("{d}\n", .{email.getSize()});

        // When full emails are downloaded, attachment information
        // is accessed using the email.NumAttachments property and
        // the email.GetMailAttach* methods.
        //
        // However, because this example downloaded headers only,
        // attachment metadata must be obtained using the IMAP object.
        const num_attach = imap.getMailNumAttach(email);

        j = 0;
        while (j < num_attach) {
            // Display the attachment filename.
            std.debug.print("{s}\n", .{try imap.getMailAttachFilename(alloc, email, j)});

            // Display the attachment size in bytes.
            const attach_size = imap.getMailAttachSize(email, j);
            std.debug.print("    size = {d} bytes\n", .{attach_size});

            j = j + 1;
        }

        std.debug.print("--\n", .{});

        i = i + 1;
    }

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