Sample code for 30+ languages & platforms
Zig

Append (Upload) an Email to an IMAP Mailbox

See more IMAP Examples

Demonstrates the Chilkat Imap.AppendMail method, which uploads an Email object to a mailbox on the server. The first argument is the destination mailbox name and the second is the Email. After a successful append, the AppendUid property holds the UID the server assigned (or 0 if none was reported). This example uploads a newly built message to the Drafts mailbox.

Background: The IMAP APPEND command lets a client add a message directly to any mailbox without sending it through SMTP — it is how "Save to Drafts" works, and how a client keeps its own copy in Sent. The initial \Seen state is governed by the AppendSeen property (or by the flag arguments of the AppendMimeWithFlags variants).

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.AppendMail method, which uploads an Email object to a mailbox on the
    // server.  The 1st argument is the destination mailbox name and the 2nd is the Email.

    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.");

    // Append (upload) the email to the "Drafts" mailbox.
    imap.appendMail("Drafts", email) catch {
        std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
        return;
    };

    // AppendUid holds the UID assigned by the server (0 if the server did not report one).
    std.debug.print("Appended UID: {d}\n", .{imap.getAppendUid()});

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