Sample code for 30+ languages & platforms
Zig

Send Encrypted Email using Certificate in Apple Keychain

See more Apple Keychain Examples

Send encrypted (S/MIME) email using a certificate found in the Apple Keychain.

Note: This example requires Chilkat v10.1.2 or later.

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

    const email = try chilkat.Email.init();
    defer email.deinit();
    email.setSubject("This email is encrypted");
    email.setBody("This is a S/MIME encrypted mail");
    email.setFrom("Joe <joe@example.com>");

    // Emails are encrypted using the recipient's certificate.
    const recipient_email_addr = "jane@example2.com";
    email.addTo("", recipient_email_addr) catch {};

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

    const cert = try chilkat.Cert.init();
    defer cert.deinit();

    // -----------------------------------------------
    // This example requires Chilkat v10.1.2 or later.
    // -----------------------------------------------

    // The recipient's certificate is used to encrypt.
    // This will load the certificate from the Apple Keychain.
    cert.loadByEmailAddress(recipient_email_addr) catch {
        std.debug.print("{s}\n", .{try cert.getLastErrorText(alloc)});
        return;
    };

    // Specify the certificate to be used for encryption.
    try email.setEncryptCert(cert);
    email.setSendEncrypted(true);

    const mailman = try chilkat.MailMan.init();
    defer mailman.deinit();
    mailman.setSmtpHost("smtp.example.com");
    mailman.setSmtpUsername("joe@example.com");
    mailman.setSmtpPort(587);
    mailman.setStartTLS(true);

    // We'll get the SMTP password from the Apple Keychain.
    // See how we originally saved the email credentials to the Keychain here:
    // Save Email Credentials in Apple Keychain or Windows Credentials Manager
    const secrets = try chilkat.Secrets.init();
    defer secrets.deinit();

    // On Windows, this is the Windows Credentials Manager
    // On MacOS/iOS, it is the Apple Keychain
    secrets.setLocation("local_manager");

    // Specify the name of the secret.
    // service and username are required.
    // appName and domain are optional.
    // Note: The values are arbitrary and can be anything you want.
    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.updateString("appName", "MyEmailApp") catch {};
    json.updateString("service", "SMTP") catch {};
    json.updateString("domain", "example.com") catch {};
    json.updateString("username", "joe@example.com") catch {};

    mailman.setSmtpPassword(try secrets.getSecretStr(alloc, json));

    // Send the encrypted email.
    // The email is encrypted and sent within the SendEmail function.
    mailman.sendEmail(email) catch {
        std.debug.print("{s}\n", .{try mailman.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("Encrypted email sent!\n", .{});
}