Sample code for 30+ languages & platforms
Zig

Sending S/MIME Encrypted Email

Sends an encrypted email using the recipient's digital certificate from a .cer file.

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

    var success: bool = false;

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    // The mailman object is used for sending and receiving email.
    const mailman = try chilkat.MailMan.init();
    defer mailman.deinit();

    // Set the SMTP server.
    mailman.setSmtpHost("smtp.comcast.net");

    // Load the .cer file into a certificate object.
    // When sending S/MIME encrypted email, it is the recipient's
    // certificate that is used for encryption.  Only the public key
    // is needed to encrypt.  The recipient is the only
    // one possessing the private key, and therefore is the only
    // one able to decrypt.
    const cert = try chilkat.Cert.init();
    defer cert.deinit();
    success = if (cert.loadFromFile("cknotes.cer")) true else |_| false;
    if (!success) {
        std.debug.print("{s}\n", .{try cert.getLastErrorText(alloc)});
        return;
    }

    // Create a new email object
    const email = try chilkat.Email.init();
    defer email.deinit();

    email.setSubject("This email is encrypted");
    email.setBody("This is a digitally encrypted mail");
    email.setFrom("Chilkat Support <support@chilkatsoft.com>");
    success = if (email.addTo("Chilkat Blog", "admin@cknotes.com")) true else |_| false;

    // Indicate that the email is to be sent encrypted.
    email.setSendEncrypted(true);

    // Specify the certificate to be used for encryption.
    success = if (email.setEncryptCert(cert)) true else |_| false;

    success = if (mailman.sendEmail(email)) true else |_| false;
    if (!success) {
        std.debug.print("{s}\n", .{try mailman.getLastErrorText(alloc)});
    } else {
        std.debug.print("Mail Sent!\n", .{});
    }
}