Sample code for 30+ languages & platforms
Zig Requires Chilkat v11.0.0+

Move Messages from one Mailbox to Another

If your IMAP server supports the MOVE capability, then it is possible to move messages from one mailbox (folder) to another. This example demonstrates the MOVE command using the SendRawCommand method. The IMAP MOVE Extension is documented in RFC 6851.

Chilkat Zig Downloads

Zig
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();

    // Use an implicit TLS connection.
    imap.setSsl(true);
    imap.setPort(993);
    imap.connect("MY-IMAP-DOMAIN") catch {
        std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
        return;
    };

    // Authenticate
    imap.login("MY-IMAP-LOGIN", "MY-IMAP-PASSWORD") catch {
        std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
        return;
    };

    // Get the list of capabilities:
    const caps = try imap.capability(alloc);
    std.debug.print("Capabilities: {s}\n", .{caps});

    // Here is an example of the string returned:
    // * CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1
    // UIDPLUS COMPRESS=DEFLATE ENABLE MOVE CONDSTORE ESEARCH UTF8=ACCEPT APPENDLIMIT=35882577
    // LIST-EXTENDED LIST-STATUS

    if (!imap.hasCapability("MOVE", caps)) {
        std.debug.print("The IMAP server does not support the MOVE extension.\n", .{});
        return;
    }

    std.debug.print("Good, the MOVE extension is supported...\n", .{});

    // Select a mailbox, search for some messages to get a sequence-set (i.e. a
    // range of message sequence numbers or UIDs.  Then move these messages to another
    // mailbox.

    imap.selectMailbox("old") catch {
        std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
        return;
    };

    // Get a set of message sequence numbers for all emails with "Gencer" in the FROM name/address.
    const b_return_uids = false;

    const msg_set = try chilkat.MessageSet.init();
    defer msg_set.deinit();
    imap.queryMbx("FROM Gencer", b_return_uids, msg_set) catch {
        std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
        return;
    };

    // The message set, as a compact string, will look something like this:
    // 1572,1876:1881,1883,1886,1895,1905:1906,1910:1911,1923,1959:1963
    const sequence_set = try msg_set.toCompactString(alloc);
    std.debug.print("{s}\n", .{sequence_set});

    // Let's form our MOVE command.
    const sb_move_cmd = try chilkat.StringBuilder.init();
    defer sb_move_cmd.deinit();
    sb_move_cmd.append("MOVE ") catch {};
    sb_move_cmd.append(sequence_set) catch {};
    sb_move_cmd.append(" old/gencer") catch {};
    std.debug.print("Sending: {s}\n", .{try sb_move_cmd.getAsString(alloc)});

    const cmd_response = imap.sendRawCommand(alloc, try sb_move_cmd.getAsString(alloc)) catch {
        std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("{s}\n", .{cmd_response});

    // The response looks like this:
    //     * 1572 EXPUNGE
    //     * 1875 EXPUNGE
    //     * 1875 EXPUNGE
    //     * 1875 EXPUNGE
    //     * 1875 EXPUNGE
    //     * 1875 EXPUNGE
    //     * 1875 EXPUNGE
    //     * 1876 EXPUNGE
    //     * 1878 EXPUNGE
    //     * 1886 EXPUNGE
    //     * 1895 EXPUNGE
    //     * 1895 EXPUNGE
    //     * 1898 EXPUNGE
    //     * 1898 EXPUNGE
    //     * 1909 EXPUNGE
    //     * 1944 EXPUNGE
    //     * 1944 EXPUNGE
    //     * 1944 EXPUNGE
    //     * 1944 EXPUNGE
    //     * 1944 EXPUNGE
    //     * 2274 EXISTS
    //     aaaf OK [COPYUID 62 1582,1886:1891,1893,1896,1905,1915:1916,1920:1921,1933,1974:1978 20,17,16,15,14,13,12,10:11,9,19,18,8,7,6,5,4,3,2,1] (Success)

    // The last line should indicate OK.

    // Disconnect from the IMAP server.
    try imap.disconnect();

    std.debug.print("All Done.\n", .{});
}