Zig Requires Chilkat v11.0.0+
Zig
Imap.GetMailSize vs Email.Size
Shows how to get the total size of an email, as well as the sizes of the attachments. This can be done when either full-emails or headers-only are downloaded.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("****", "****") 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;
};
// Get the message IDs of all the emails in the mailbox
// We can choose to fetch UIDs or sequence numbers.
const fetch_uids = true;
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;
};
// When downloading headers, each email object contains
// (obviously) the headers, but the body will be missing.
// Attachments will not be included. However, it is
// possible to get information about the attachments
// as well as the complete size of the email.
const bundle = try chilkat.EmailBundle.init();
defer bundle.deinit();
const headers_only = true;
imap.fetchMsgSet(headers_only, message_set, bundle) catch {
std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
return;
};
// Loop over the email objects and display information about each:
const email = try chilkat.Email.init();
defer email.deinit();
var i: i32 = 0;
var j: i32 = 0;
const num_emails = bundle.getMessageCount();
while (i < num_emails) {
bundle.emailAt(i, email) catch {};
// Display the From and Subject
std.debug.print("{s}\n", .{try email.getFrom(alloc)});
std.debug.print("{s}\n", .{try email.getSubject(alloc)});
// Display the recipients:
j = 0;
while (j < email.getNumTo()) {
std.debug.print("TO: {s}, {s}\n", .{ try email.getToName(alloc, j), try email.getToAddr(alloc, j) });
j = j + 1;
}
j = 0;
while (j < email.getNumCC()) {
std.debug.print("CC: {s}, {s}\n", .{ try email.getCcName(alloc, j), try email.getCcAddr(alloc, j) });
j = j + 1;
}
// 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);
j = 0;
while (j < num_attach) {
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});
j = j + 1;
}
std.debug.print("--\n", .{});
i = i + 1;
}
// Disconnect from the IMAP server.
try imap.disconnect();
}