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

Delete IMAP Email

To delete an email, the "Deleted" flag must be set to 1 for each message to be deleted. You must also call Expunge or ExpungeAndClose to remove the emails marked as deleted.

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.

    // This example deletes email matching a criteria from Inbox.
    const imap = try chilkat.Imap.init();
    defer imap.deinit();

    // Turn on session logging:
    imap.setKeepSessionLog(true);

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

    const fetch_uids = true;
    // Get the message IDs for all emails having "FTP2" in the subject.
    const message_set = try chilkat.MessageSet.init();
    defer message_set.deinit();
    imap.queryMbx("SUBJECT FTP2", fetch_uids, message_set) catch {
        std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
        return;
    };

    // Set the Deleted flag for each message:
    imap.setFlags(message_set, "Deleted", 1) catch {
        std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
        return;
    };

    // Expunge and close the mailbox.
    imap.expungeAndClose() catch {
        std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
        return;
    };

    // Display the session log.
    std.debug.print("{s}\n", .{try imap.getSessionLog(alloc)});

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