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

Get IMAP UID from Email Header

See more IMAP Examples

Demonstrates how to get the IMAP UID from an email header. After fetching an email or header using IMAP, the UID is contained in the ckx-imap-uid header field.

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 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("login", "password") 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 fetch_uids = true;
    // Get the message UIDs of all the emails in the mailbox

    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;
    };

    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;
    };

    // The UID of each fetched email header is available
    // in the ckx-imap-uid header field.
    const email = try chilkat.Email.init();
    defer email.deinit();
    const msg_set1 = try chilkat.MessageSet.init();
    defer msg_set1.deinit();
    var i: i32 = 0;
    const sz_bundle = bundle.getMessageCount();
    while (i < sz_bundle) {
        bundle.emailAt(i, email) catch {};

        // Build a message set containing one UID
        const uid_str = try email.getHeaderField(alloc, "ckx-imap-uid");
        msg_set1.fromCompactString(uid_str) catch {};

        // Move this message to some other folder.
        imap.moveMessages(msg_set1, "someOtherFolder") catch {
            std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
            // Exit the loop...
            i = sz_bundle;
        };

        i = i + 1;
    }

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