Zig Requires Chilkat v11.0.0+
Zig
Delete All IMAP Email
Demonstrates two ways to delete all email in a mailbox on an IMAP server.Chilkat Zig Downloads
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.
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.RubyMail") catch {
std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
return;
};
// Get the complete set of Uids for email in the selected mailbox.
const message_set = try chilkat.MessageSet.init();
defer message_set.deinit();
imap.queryMbx("ALL", true, message_set) catch {
std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
return;
};
// Set the Deleted flag for each message.
// (ExpungeAndClose must be called to finalize the delete.)
imap.setFlags(message_set, "Deleted", 1) catch {
std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
return;
};
// Alternatively, the Deleted flag may be set for each UID
// individiually, but this is less efficient:
var i: i32 = 0;
const n = message_set.getCount();
while (i < n) {
imap.setFlag(message_set.getId(i), message_set.getHasUids(), "Deleted", 1) catch {
std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
return;
};
i = i + 1;
}
// 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();
}