Zig Requires Chilkat v11.0.0+
Zig
Copy an Email from One Mailbox to Another
Copies an email from one IMAP folder to another. After running this example, copies of the email will be present in both source and destination folders.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.
// This example copies an email from one mailbox to another.
const imap = try chilkat.Imap.init();
defer imap.deinit();
// Turn on session logging:
imap.setKeepSessionLog(true);
// 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.testing.a") catch {
std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
return;
};
const fetch_uids = true;
// Get the message IDs for all emails having "Re:" in the subject.
const message_set = try chilkat.MessageSet.init();
defer message_set.deinit();
imap.queryMbx("SUBJECT Re:", fetch_uids, message_set) catch {
std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
return;
};
// Copy the messages from "Inbox.testing.a" to "Inbox.testing.b" in one call to CopyMultiple:
imap.copyMultiple(message_set, "Inbox.testing.b") catch {
std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
return;
};
// Alternatively, loop over each message in the set and
// copy each separately:
var i: i32 = 0;
while (i < message_set.getCount()) {
const msg_id = @as(i32, @intCast(message_set.getId(i)));
const is_uid = message_set.getHasUids();
imap.copy(@as(u32, @intCast(msg_id)), is_uid, "Inbox.testing.c") catch {
std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
return;
};
i = i + 1;
}
// Display the session log.
std.debug.print("{s}\n", .{try imap.getSessionLog(alloc)});
// Disconnect from the IMAP server.
try imap.disconnect();
}