Sample code for 30+ languages & platforms
Rust

Set Cert and Private Key for S/MIME Decryption

See more IMAP Examples

Demonstrates the Chilkat Imap.SetDecryptCert2 method, which specifies a certificate together with a separately supplied private key for decrypting S/MIME messages. The first argument is the Cert and the second is the PrivateKey. This example loads the certificate and key from separate files.

Note: The certificate path is relative to the application's current working directory. Supply the path to your own certificate file.

Background: Sometimes the certificate (the public part) and the private key are stored separately — a .cer alongside a PEM key file, for instance. This method pairs them explicitly, whereas SetDecryptCert expects a single Cert that already provides its private key. After the pairing, subsequently downloaded S/MIME messages are decrypted automatically.

Chilkat Rust Downloads

Rust

// Demonstrates the Imap.SetDecryptCert2 method, which specifies a certificate together with a
// separately supplied private key for decrypting S/MIME messages.  The 1st argument is the
// Cert and the 2nd is the PrivateKey.

let imap = chilkat::Imap::new();

imap.set_ssl(true);
imap.set_port(993);

if imap.connect("imap.example.com").is_err() {
    println!("{}", imap.last_error_text());
    return;
}

if imap.login("user@example.com", "myPassword").is_err() {
    println!("{}", imap.last_error_text());
    return;
}

// Load the certificate and its private key from separate files.
let cert = chilkat::Cert::new();
if cert.load_from_file("qa_data/smime.cer").is_err() {
    println!("{}", cert.last_error_text());
    return;
}

let priv_key = chilkat::PrivateKey::new();
if priv_key.load_pem_file("qa_data/smime_privkey.pem").is_err() {
    println!("{}", priv_key.last_error_text());
    return;
}

// Use the cert and key to automatically decrypt subsequently downloaded S/MIME messages.
if imap.set_decrypt_cert2(&cert, &priv_key).is_err() {
    println!("{}", imap.last_error_text());
    return;
}

if imap.disconnect().is_err() {
    println!("{}", imap.last_error_text());
    return;
}