Sample code for 30+ languages & platforms
Zig

Append MIME from a StringBuilder with Flags

See more IMAP Examples

Demonstrates the Chilkat Imap.AppendMimeWithFlagsSb method, which uploads MIME contained in a StringBuilder and sets its initial system flags. The first argument is the mailbox, the second is the StringBuilder, and the third through sixth set \Seen, \Flagged, \Answered, and \Draft. This example uploads to Drafts with the draft flag set.

Background: This is the StringBuilder form of AppendMimeWithFlags. Passing the MIME as a StringBuilder avoids copying a large message into an intermediate string and is convenient when you already built or loaded the message into a StringBuilder (for example with LoadFile).

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

    // Demonstrates the Imap.AppendMimeWithFlagsSb method, which uploads MIME contained in a
    // StringBuilder and sets its initial system flags.  The 1st argument is the mailbox, the 2nd
    // is the StringBuilder, and the 3rd through 6th set \Seen, \Flagged, \Answered, and \Draft.

    const imap = try chilkat.Imap.init();
    defer imap.deinit();

    imap.setSsl(true);
    imap.setPort(993);

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

    imap.login("user@example.com", "myPassword") catch {
        std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
        return;
    };

    // Build the email to be uploaded.
    const email = try chilkat.Email.init();
    defer email.deinit();
    email.setSubject("Meeting agenda");
    email.setFrom("Alice <alice@example.com>");
    try email.addTo("Bob", "bob@example.com");
    email.setBody("Let's meet at 10am to review the agenda.");

    const mime_text = email.getMime(alloc) catch {
        std.debug.print("{s}\n", .{try email.getLastErrorText(alloc)});
        return;
    };

    // Load the MIME into a StringBuilder.  (A StringBuilder can also be filled via LoadFile.)
    const sb_mime = try chilkat.StringBuilder.init();
    defer sb_mime.deinit();
    sb_mime.append(mime_text) catch {};

    // Choose the initial flags.  Named variables make each argument's meaning clear at the call.
    const seen = false;
    const flagged = false;
    const answered = false;
    const draft = true;

    // Upload to "Drafts" with the \Draft flag set.
    imap.appendMimeWithFlagsSb("Drafts", sb_mime, seen, flagged, answered, draft) catch {
        std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("Appended UID: {d}\n", .{imap.getAppendUid()});

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