Sample code for 30+ languages & platforms
Zig Requires Chilkat v10.1.2+

Send Signed Email using PFX File (long version)

Demonstrates how to send a signed email using a digital certificate w/ private key stored in a PFX 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.mymailserver.com");

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

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

    // Indicate that the email should be sent signed.
    email.setSendSigned(true);

    // Load a PFX file into a certificate store object.
    // Then locate the certificate matching the  sender's email address,
    // and use it for signing.
    // (a PFX file may contain more than one certificate.)
    const cert_store = try chilkat.CertStore.init();
    defer cert_store.deinit();
    // The 1st argument is the path of the PFX file, the 2nd arg is the
    // PFX file's password:
    success = if (cert_store.loadPfxFile("/pfx_files/chilkatsoft_secret.pfx", "secret")) true else |_| false;
    if (!success) {
        std.debug.print("{s}\n", .{try cert_store.getLastErrorText(alloc)});
        return;
    }

    // Find the certificate for the email address:
    const json_e = try chilkat.JsonObject.init();
    defer json_e.deinit();
    json_e.updateString("email", "admin@chilkatsoft.com") catch {};

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

    // Alternatively, if a PFX file is known to contain a single certificate,
    // you may load it directly into a Chilkat certificate object.
    // This snippet of source code shows how:
    const cert2 = try chilkat.Cert.init();
    defer cert2.deinit();
    // The 1st argument is the filename, the 2nd arg is the
    // PFX file's password:
    success = if (cert2.loadPfxFile("/pfx_files/chilkatsoft_secret.pfx", "secret")) true else |_| false;
    if (!success) {
        std.debug.print("{s}\n", .{try cert2.getLastErrorText(alloc)});
        return;
    }

    // This example will use the cert from the certStore...
    success = if (email.setSigningCert(cert)) true else |_| false;

    // Signed email can be sent in two different ways.
    // In a multipart/signed email, the signature is attached as a separate MIME part.
    // In an opaque email (signedData) the content of the email is encapsulated within the signature
    // and the email is sent as "application/pkcs7-mime".
    // Either should be fine, but some receiving systems might require one or the other..
    mailman.setOpaqueSigning(false);

    // Send a signed email.
    success = if (mailman.sendEmail(email)) true else |_| false;
    if (!success) {
        std.debug.print("{s}\n", .{try mailman.getLastErrorText(alloc)});
    } else {
        // The LastErrorText property provides information
        // even when successful.
        std.debug.print("{s}\n", .{try mailman.getLastErrorText(alloc)});
        std.debug.print("Mail Sent!\n", .{});
    }
}