Sample code for 30+ languages & platforms
Zig

Render an Email to MIME in a StringBuilder

See more SMTP Examples

Demonstrates the Chilkat MailMan.RenderToMimeSb method, which renders an Email object as MIME text and appends the result to a StringBuilder, without sending the email. This example renders a message into a StringBuilder and prints it.

Background: This is the StringBuilder version of RenderToMime. Appending into a StringBuilder is more efficient when the MIME is large or when you plan to further inspect or manipulate it — searching, replacing, or accumulating additional text — without creating extra intermediate string copies.

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 MailMan.RenderToMimeSb method, which renders an Email object as MIME text
    // and appends the result to a StringBuilder (without sending the email).

    const mailman = try chilkat.MailMan.init();
    defer mailman.deinit();

    // Build the email to render.
    const email = try chilkat.Email.init();
    defer email.deinit();
    email.setSubject("Rendered email");
    email.setFrom("alice@example.com");
    email.addTo("Bob", "bob@example.com") catch {};
    email.setBody("This message is rendered to MIME in a StringBuilder.");

    // Render the MIME into a StringBuilder.
    const sb_mime = try chilkat.StringBuilder.init();
    defer sb_mime.deinit();

    mailman.renderToMimeSb(email, sb_mime) catch {
        std.debug.print("{s}\n", .{try mailman.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("{s}\n", .{try sb_mime.getAsString(alloc)});
}