Zig Requires Chilkat v11.0.0+
Zig
Fetch Oldest/Newest IMAP Email
Emails may be downloaded by sequence number. Assuming the selected mailbox is not empty, the oldest email is at sequence number 1, and the newest email is at sequence number N. The FetchSingle method may be used to download by sequence number.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("mail.testemail.net") catch {
std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
return;
};
// Login
imap.login("***", "***") 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;
};
// After selecting a mailbox, the NumMessages property
// contains the number of emails in the selected mailbox.
const n = imap.getNumMessages();
if (n > 0) {
// The oldest email is always at sequence number 1.
const is_uid = false;
const oldest_email = try chilkat.Email.init();
defer oldest_email.deinit();
imap.fetchEmail(false, 1, is_uid, oldest_email) catch {
std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
return;
};
// Display the From and Subject
std.debug.print("{s}\n", .{try oldest_email.getFromAddress(alloc)});
std.debug.print("{s}\n", .{try oldest_email.getSubject(alloc)});
std.debug.print("--\n", .{});
// The newest email is at sequence number N:
const newest_email = try chilkat.Email.init();
defer newest_email.deinit();
imap.fetchEmail(false, @as(u32, @intCast(n)), is_uid, newest_email) catch {
std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
return;
};
// Display the From and Subject
std.debug.print("{s}\n", .{try newest_email.getFromAddress(alloc)});
std.debug.print("{s}\n", .{try newest_email.getSubject(alloc)});
}
// Disconnect from the IMAP server.
try imap.disconnect();
}