Sample code for 30+ languages & platforms
Rust

Find Certificate for Email Encryption

Demonstrates finding the recipient's certificate in the Windows certificate store and using it to send encrypted email.

Chilkat Rust Downloads

Rust

// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

let mailman = chilkat::MailMan::new();

// Set the SMTP server.
mailman.set_smtp_host("smtp.example.com");

// Create a new email object
let email = chilkat::Email::new();

email.set_subject("This email is encrypted");
email.set_body("This is a digitally encrypted mail");
email.set_from("Joe <joe@example.com>");
// Emails are encrypted using the recipient's certificate.
let recipient_email_addr = "jane@example2.com".to_string();
let _ = email.add_to("Jane", &recipient_email_addr);

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

// This example demonstrates finding the email encryption certificate
// on a Windows system where the certificate is stored in the Windows 
// certificate store.

let cert = chilkat::Cert::new();
// The recipient's certificate is used to encrypt.
// (Because the recipient is the only one in possession of the private key to decrypt.)
if cert.load_by_email_address(&recipient_email_addr).is_err() {
    println!("{}", cert.last_error_text());
    return;
}

// Specify the certificate to be used for encryption.
let _ = email.set_encrypt_cert(&cert).is_ok();

if mailman.send_email(&email).is_err() {
    println!("{}", mailman.last_error_text());
} else {
    println!("Encrypted Mail Sent!");
}