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

Reading Unread POP3 Email

The POP3 protocol cannot determine which emails are "unread," and pure POP3 servers do not store this information. Servers like Exchange Server, offering both POP3 and IMAP interfaces, do contain read/unread data, but it's only accessible through IMAP. Email clients like Outlook and Thunderbird store read/unread statuses on the client side. The example demonstrates using UIDLs to track and manage "unread" emails.

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

    // Keep a records of already-seen UIDLs in hash table serialized to an XML file.
    const seen_uidls_path = "c:/temp/seenUidls.xml";

    const sb_xml = try chilkat.StringBuilder.init();
    defer sb_xml.deinit();
    const ht_seen_uidls = try chilkat.Hashtable.init();
    defer ht_seen_uidls.deinit();
    const fac = try chilkat.FileAccess.init();
    defer fac.deinit();
    if (fac.fileExists(seen_uidls_path)) {
        sb_xml.loadFile(seen_uidls_path, "utf-8") catch {
            std.debug.print("{s}\n", .{try sb_xml.getLastErrorText(alloc)});
            return;
        };

        ht_seen_uidls.addFromXmlSb(sb_xml) catch {};
    }

    // Get the complete list of UIDLs on the mail server.
    const st_uidls = try chilkat.StringTable.init();
    defer st_uidls.deinit();
    mailman.fetchUidls(st_uidls) catch {
        std.debug.print("{s}\n", .{try mailman.getLastErrorText(alloc)});
        return;
    };

    // Build a list of unseen UIDLs
    const st_unseen_uidls = try chilkat.StringTable.init();
    defer st_unseen_uidls.deinit();

    var uidl: [:0]const u8 = "";
    var i: i32 = 0;
    var count: i32 = st_uidls.getCount();
    while (i < count) {
        uidl = try st_uidls.stringAt(alloc, i);
        if (!ht_seen_uidls.contains(uidl)) {
            st_unseen_uidls.append(uidl) catch {};
        }

        i = i + 1;
    }

    if (st_unseen_uidls.getCount() == 0) {
        std.debug.print("No unseen emails!\n", .{});
        return;
    }

    // Download the unseen emails, adding each UIDL to the "seen" hash table.
    const email = try chilkat.Email.init();
    defer email.deinit();

    count = st_unseen_uidls.getCount();
    i = 0;
    while (i < count) {
        // Download the full email.
        uidl = try st_unseen_uidls.stringAt(alloc, i);
        mailman.fetchByUidl(uidl, false, 0, email) catch {
            std.debug.print("{s}\n", .{try mailman.getLastErrorText(alloc)});
            return;
        };

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

        // Add this UIDL to the "seen" hash table.
        ht_seen_uidls.addStr(uidl, "") catch {};

        i = i + 1;
    }

    mailman.pop3EndSession() catch {};

    // Update the "seen" UIDLs file.
    sb_xml.clear();
    ht_seen_uidls.toXmlSb(sb_xml) catch {};
    sb_xml.writeFile(seen_uidls_path, "utf-8", false) catch {
        std.debug.print("{s}\n", .{try sb_xml.getLastErrorText(alloc)});
        return;
    };

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