Zig Requires Chilkat v11.0.0+
Zig
How to Copy IMAP Mail to another IMAP Server
Demonstrates how to copy the entire contents of an IMAP mailbox to another IMAP 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_src = try chilkat.Imap.init();
defer imap_src.deinit();
// Connect to our source IMAP server.
imap_src.connect("imap.example.com") catch {
std.debug.print("{s}\n", .{try imap_src.getLastErrorText(alloc)});
return;
};
// Login to the source IMAP server
imap_src.login("admin@chilkatsoft.com", "myPassword") catch {
std.debug.print("{s}\n", .{try imap_src.getLastErrorText(alloc)});
return;
};
const imap_dest = try chilkat.Imap.init();
defer imap_dest.deinit();
// Connect to our destination IMAP server.
imap_dest.connect("mail.example-code.com") catch {
std.debug.print("{s}\n", .{try imap_dest.getLastErrorText(alloc)});
return;
};
// Login to the destination IMAP server
imap_dest.login("myLogin", "myPassword") catch {
std.debug.print("{s}\n", .{try imap_dest.getLastErrorText(alloc)});
return;
};
// Select an IMAP mailbox on the source IMAP server
imap_src.selectMailbox("Inbox") catch {
std.debug.print("{s}\n", .{try imap_src.getLastErrorText(alloc)});
return;
};
// After selecting a mailbox, the NumMessages property will
// be updated to reflect the total number of emails in the mailbox:
std.debug.print("{d}\n", .{imap_src.getNumMessages()});
// The emails in the mailbox will always have sequence numbers
// ranging from 1 to NumMessages.
// This example will copy the first 10 messages using sequence numbers
const sb_mime = try chilkat.StringBuilder.init();
defer sb_mime.deinit();
var seq_num: i32 = 0;
seq_num = 1;
while (seq_num <= 10) : (seq_num += 1) {
sb_mime.clear();
imap_src.fetchSingleAsMimeSb(@as(u32, @intCast(seq_num)), false, sb_mime) catch {
std.debug.print("{s}\n", .{try imap_src.getLastErrorText(alloc)});
return;
};
imap_dest.appendMimeWithFlagsSb("Inbox", sb_mime, false, false, false, false) catch {
std.debug.print("{s}\n", .{try imap_dest.getLastErrorText(alloc)});
return;
};
}
// Disconnect from the IMAP servers.
try imap_src.disconnect();
try imap_dest.disconnect();
}