Sample code for 30+ languages & platforms
Rust

Sending S/MIME Encrypted Email

Sends an encrypted email using the recipient's digital certificate from a .cer file.

Chilkat Rust Downloads

Rust

// 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.
let mailman = chilkat::MailMan::new();

// Set the SMTP server.
mailman.set_smtp_host("smtp.comcast.net");

// Load the .cer file into a certificate object.
// When sending S/MIME encrypted email, it is the recipient's
// certificate that is used for encryption.  Only the public key
// is needed to encrypt.  The recipient is the only
// one possessing the private key, and therefore is the only
// one able to decrypt.
let cert = chilkat::Cert::new();
if cert.load_from_file("cknotes.cer").is_err() {
    println!("{}", cert.last_error_text());
    return;
}

// 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("Chilkat Support <support@chilkatsoft.com>");
let _ = email.add_to("Chilkat Blog", "admin@cknotes.com").is_ok();

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

// 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!("Mail Sent!");
}