Zig Requires Chilkat v11.0.0+
Zig
Delete Email Individually (One at a time) from an IMAP Mailbox
Downloads email from an IMAP mailbox and deletes emails individually (one by one).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 assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
const imap = try chilkat.Imap.init();
defer imap.deinit();
// 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;
};
// We can choose to fetch UIDs or sequence numbers.
const fetch_uids = true;
// Get the message IDs of all the emails in the mailbox
const message_set = try chilkat.MessageSet.init();
defer message_set.deinit();
imap.queryMbx("ALL", fetch_uids, message_set) catch {
std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
return;
};
// Fetch the emails into a bundle object:
const bundle = try chilkat.EmailBundle.init();
defer bundle.deinit();
const headers_only = false;
imap.fetchMsgSet(headers_only, message_set, bundle) catch {
std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
return;
};
// To mark a complete set of emails for deletion, call SetFlags:
imap.setFlags(message_set, "Deleted", 1) catch {
std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
return;
};
// Messages can also be marked for deletion individually:
// Loop over the bundle and mark each message for deletion.
const email = try chilkat.Email.init();
defer email.deinit();
var i: i32 = 0;
const num_emails = bundle.getMessageCount();
while (i < num_emails) {
bundle.emailAt(i, email) catch {};
std.debug.print("{s}\n", .{try email.getFrom(alloc)});
std.debug.print("{s}\n", .{try email.getSubject(alloc)});
// To delete this email, set the "Deleted" flag to 1.
// The email is not actually deleted until Expunge or
// ExpungeAndClose is called.
imap.setMailFlag(email, "Deleted", 1) catch {
std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
return;
};
std.debug.print("--\n", .{});
i = i + 1;
}
imap.expungeAndClose() catch {
std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
return;
};
// Disconnect from the IMAP server.
try imap.disconnect();
}