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

Delete Individual Emails from POP3 Mailbox

Demonstrates how to delete individual emails from a POP3 mailbox.

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("myLogin");
    mailman.setPopPassword("myPassword");

    // Set the ImmediateDelete property = false
    // When the DeleteEmail method is called, the POP3 session
    // will remain open and the emails will be deleted all at once
    // when the session closes.
    mailman.setImmediateDelete(false);

    // Download email headers.
    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 {};

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

        // If you decide the email is to be deleted, call DeleteEmail.
        // This marks the email for deletion on the POP3 server.
        mailman.deleteEmail(email) catch {
            std.debug.print("{s}\n", .{try mailman.getLastErrorText(alloc)});
            return;
        };

        i = i + 1;
    }

    // Make sure the POP3 session is ended to finalize the deletes.
    mailman.pop3EndSession() catch {
        std.debug.print("{s}\n", .{try mailman.getLastErrorText(alloc)});
        return;
    };

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