Sample code for 30+ languages & platforms
Zig

Send Encrypted Email to Multiple Recipients

Demonstrates how to create and send an S/MIME encrypted email to multiple recipients. The digital certificate of each recipient is required. The encrypting/sending process uses each recipient's digital certificate (which internally contains the public key). Each recipient decrypts the received email using his/her private key.

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

    // Load each recipient's certificate into a Chilkat certificate object.
    // This example loads the certificates from files.  However, the Chilkat
    // certificate object provides other means for loading certificates,
    // such as from in-memory PEM strings, or in-memory binary DER encoded form, etc.
    const cert1 = try chilkat.Cert.init();
    defer cert1.deinit();
    success = if (cert1.loadFromFile("recipient1.cer")) true else |_| false;
    if (!success) {
        std.debug.print("{s}\n", .{try cert1.getLastErrorText(alloc)});
        return;
    }

    const cert2 = try chilkat.Cert.init();
    defer cert2.deinit();
    success = if (cert2.loadFromFile("recipient2.cer")) true else |_| false;
    if (!success) {
        std.debug.print("{s}\n", .{try cert2.getLastErrorText(alloc)});
        return;
    }

    const cert3 = try chilkat.Cert.init();
    defer cert3.deinit();
    success = if (cert3.loadFromFile("recipient3.cer")) true else |_| false;
    if (!success) {
        std.debug.print("{s}\n", .{try cert3.getLastErrorText(alloc)});
        return;
    }

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

    email.setSubject("This email is encrypted and sent to 3 recipients");
    email.setBody("This is an S/MIME encrypted mail sent to 3 recipients");
    email.setFrom("Chilkat Support <support@chilkatsoft.com>");

    // Make each of the certificates available for encrypting the email
    // by calling AddEncryptCert for each.
    success = if (email.addEncryptCert(cert1)) true else |_| false;
    if (success) {
        success = if (email.addEncryptCert(cert2)) true else |_| false;
    }

    if (success) {
        success = if (email.addEncryptCert(cert3)) true else |_| false;
    }

    if (!success) {
        std.debug.print("{s}\n", .{try email.getLastErrorText(alloc)});
        return;
    }

    // Add 3 recipients to the email (2 TO addresses, and 1 CC address)
    success = if (email.addTo("Recipient 1", "admin@cknotes.com")) true else |_| false;
    success = if (email.addTo("Recipient 2", "somebody001122@yahoo.com")) true else |_| false;
    success = if (email.addCC("Recipient 3", "somebody123xyz@gmail.com")) true else |_| false;

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

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