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

Read POP3 Mail Headers

Reads the header for each email in a POP3 mailbox and display the FROM and SUBJECT header fields.

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.

    // The mailman object is used for receiving (POP3)
    // and sending (SMTP) email.
    const mailman = try chilkat.MailMan.init();
    defer mailman.deinit();

    // Set the POP3 server's hostname
    mailman.setMailHost("pop.example.com");

    // Set the POP3 login/password.
    mailman.setPopUsername("****");
    mailman.setPopPassword("****");

    // Read mail headers and one line of the body.
    const bundle = try chilkat.EmailBundle.init();
    defer bundle.deinit();
    const keep_on_server = true;
    const headers_only = true;
    const num_body_lines = 1;
    mailman.fetchAll(keep_on_server, headers_only, num_body_lines, bundle) catch {
        std.debug.print("{s}\n", .{try mailman.getLastErrorText(alloc)});
        return;
    };

    const email = try chilkat.Email.init();
    defer email.deinit();
    var i: i32 = 0;
    while (i < bundle.getMessageCount()) {
        bundle.emailAt(i, email) catch {};

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

        i = i + 1;
    }
}