Sample code for 30+ languages & platforms
Zig

Set the Decryption Certificate for an Email

See more Email Object Examples

Demonstrates the Chilkat Email.SetDecryptCert method, which explicitly provides a certificate (with access to its private key) for decrypting a received encrypted email. Set the certificate before loading the encrypted message so Chilkat can decrypt it automatically. This example loads the certificate from a PFX, sets it, and then loads an encrypted email.

Background: Decrypting S/MIME email requires the recipient's private key. On Windows and macOS, Chilkat can find an installed certificate automatically, but when the key lives in a standalone PFX file you provide it explicitly with SetDecryptCert. A PFX (PKCS#12) bundles the certificate and its private key in one password-protected file. If the certificate and key are in separate files, use SetDecryptCert2 instead.

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 SetDecryptCert method, which explicitly provides a certificate (with
    // access to its private key) for decrypting a received encrypted email.  Set the cert
    // before loading the encrypted email so it can be decrypted automatically.

    const email = try chilkat.Email.init();
    defer email.deinit();

    // Load a certificate together with its private key from a PFX file.
    const cert = try chilkat.Cert.init();
    defer cert.deinit();
    cert.loadPfxFile("qa_data/certs/decryption.pfx", "pfx_password") catch {
        std.debug.print("{s}\n", .{try cert.getLastErrorText(alloc)});
        return;
    };

    // Provide the certificate to use for decryption.
    email.setDecryptCert(cert) catch {
        std.debug.print("{s}\n", .{try email.getLastErrorText(alloc)});
        return;
    };

    // Load the encrypted email; Chilkat decrypts it using the certificate.
    email.loadEml("qa_data/eml/encrypted.eml") catch {
        std.debug.print("{s}\n", .{try email.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("ReceivedEncrypted = {}\n", .{email.getReceivedEncrypted()});
    std.debug.print("Decrypted = {}\n", .{email.getDecrypted()});

    // Note: Paths such as "qa_data/..." are relative local filesystem paths,
    // relative to the current working directory of the running application.
}