Zig Requires Chilkat v11.0.0+
Zig
Fetch Single Email by UID or Sequence Number
Assuming the UID is known, download a single email by UID from an IMAP mail 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();
// 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("***", "***") 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 email = try chilkat.Email.init();
defer email.deinit();
const uid = 2014;
const is_uid = true;
imap.fetchEmail(false, @as(u32, @intCast(uid)), is_uid, email) catch {
std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
return;
};
// Display the From and Subject
std.debug.print("{s}\n", .{try email.getFromAddress(alloc)});
std.debug.print("{s}\n", .{try email.getSubject(alloc)});
// Display the Body property, which is the default body.
// If an email has an HTML body, the Body property contains
// the HTML source. Otherwise it contains the plain-text
// body.
std.debug.print("---- EMAIL BODY ----\n", .{});
std.debug.print("{s}\n", .{try email.getBody(alloc)});
std.debug.print("--------------------\n", .{});
// Display the recipients:
var j: i32 = 0;
j = 0;
while (j < email.getNumTo()) : (j += 1) {
std.debug.print("{s}, {s}\n", .{ try email.getToName(alloc, j), try email.getToAddr(alloc, j) });
}
j = 0;
while (j < email.getNumCC()) : (j += 1) {
std.debug.print("{s}, {s}\n", .{ try email.getCcName(alloc, j), try email.getCcAddr(alloc, j) });
}
// Show the total size of the email, including body and attachments:
std.debug.print("{d}\n", .{email.getSize()});
// When a full email is downloaded, we would use the
// email.NumAttachments property in conjunction with the
// email.GetMailAttach* methods.
// However, when an email object contains only the header,
// we need to access the attachment info differently:
const num_attach = imap.getMailNumAttach(email);
std.debug.print("{d}\n", .{num_attach});
j = 0;
while (j < num_attach) : (j += 1) {
std.debug.print("{s}\n", .{try imap.getMailAttachFilename(alloc, email, j)});
const attach_size = imap.getMailAttachSize(email, j);
std.debug.print(" size = {d} bytes\n", .{attach_size});
}
std.debug.print("--\n", .{});
// Disconnect from the IMAP server.
try imap.disconnect();
}