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

Download MIME Source of Emails in IMAP Mailbox

Demonstrates how to download the MIME source for emails on an IMAP server.

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

    // The NumMessages property contains the number of messages in the selected mailbox.
    const num_messages = imap.getNumMessages();
    if (num_messages == 0) {
        std.debug.print("No messages exist in the Inbox.\n", .{});
        return;
    }

    const sb_mime = try chilkat.StringBuilder.init();
    defer sb_mime.deinit();
    var seq_num: i32 = 0;
    seq_num = 1;
    while (seq_num <= num_messages) : (seq_num += 1) {
        sb_mime.clear();
        imap.fetchSingleAsMimeSb(@as(u32, @intCast(seq_num)), false, sb_mime) catch {
            std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
            return;
        };

        // The MIME source of the downloaded email is contained in sbMime.
    }

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