Zig Requires Chilkat v11.0.0+
Zig
Subscribe to Mailboxes and List Subscribed Mailboxes
See more IMAP Examples
Demonstrates how to list subscribed mailboxes, and unsubscribe/subscribe to a mailbox.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;
};
// First examine the already subscribed mailboxes.
const ref_name = "";
const wildcarded_mailbox = "*";
const subscribed = true;
const mboxes = try chilkat.Mailboxes.init();
defer mboxes.deinit();
imap.mbxList(subscribed, ref_name, wildcarded_mailbox, mboxes) catch {
std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
return;
};
var i: i32 = 0;
while (i < mboxes.getCount()) {
std.debug.print("{s}\n", .{try mboxes.getName(alloc, i)});
i = i + 1;
}
std.debug.print("----\n", .{});
// Sample output.
// INBOX
// INBOX/recent
// INBOX/misc
// INBOX/misc/solutions
// ...
// Outbox
// Deleted Items
// Sent Items
// Drafts
// Junk E-mail
// Trash
// Sent
// Templates
// Let's unsubscribe to "INBOX/misc/solutions".
imap.unsubscribe("INBOX/misc/solutions") catch {
std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
return;
};
// Get the list of subscribed mailboxes again to verify that "INBOX/misc/solutions" is missing.
imap.mbxList(subscribed, "", "INBOX/misc*", mboxes) catch {
std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
return;
};
i = 0;
while (i < mboxes.getCount()) {
std.debug.print("{s}\n", .{try mboxes.getName(alloc, i)});
i = i + 1;
}
std.debug.print("----\n", .{});
// Re-subscribe to "INBOX/misc/solutions".
imap.subscribe("INBOX/misc/solutions") catch {
std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
return;
};
// Get the list of subscribed mailboxes again to verify that "INBOX/misc/solutions" is back in the list.
imap.mbxList(subscribed, "", "INBOX/misc*", mboxes) catch {
std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
return;
};
i = 0;
while (i < mboxes.getCount()) {
std.debug.print("{s}\n", .{try mboxes.getName(alloc, i)});
i = i + 1;
}
std.debug.print("----\n", .{});
// Disconnect from the IMAP server.
try imap.disconnect();
}