Sample code for 30+ languages & platforms
Zig

Get the Total Size of an IMAP Message

See more IMAP Examples

Demonstrates the Chilkat Imap.GetMailSize method, which returns the complete server-reported size of a message in bytes, including attachment data. The only argument is the Email. This example fetches only the headers and prints the full message size.

Background: The server reports each message's total size, so this value is available even when only the headers were downloaded. That lets a client show a size column in a message list, sort by size, or warn the user before downloading an unusually large message — all without fetching the full body first.

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

    // Demonstrates the Imap.GetMailSize method, which returns the complete server-reported size
    // of a message in bytes, including attachments.  The only argument is the Email.

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

    imap.selectMailbox("Inbox") catch {
        std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
        return;
    };

    // Fetch only the message headers.  Attachment bodies are NOT downloaded, but the ckx-imap-*
    // metadata describing the attachments is present, so the attachment info methods still work.
    const headers_only = true;
    const use_uid = false;
    const seq_num = 1;
    const email = try chilkat.Email.init();
    defer email.deinit();
    imap.fetchEmail(headers_only, @as(u32, @intCast(seq_num)), use_uid, email) catch {
        std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
        return;
    };

    const size_bytes = imap.getMailSize(email);
    std.debug.print("Total message size: {d} bytes\n", .{size_bytes});

    imap.disconnect() catch {
        std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
        return;
    };
}