Rust
Rust
Send Encrypted Email to Multiple Recipients
Demonstrates how to create and send an S/MIME encrypted email to multiple recipients. The digital certificate of each recipient is required. The encrypting/sending process uses each recipient's digital certificate (which internally contains the public key). Each recipient decrypts the received email using his/her private key.Chilkat Rust Downloads
let mut success = 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.
let mailman = chilkat::MailMan::new();
// Set the SMTP server.
mailman.set_smtp_host("smtp.mymailserver.com");
// Load each recipient's certificate into a Chilkat certificate object.
// This example loads the certificates from files. However, the Chilkat
// certificate object provides other means for loading certificates,
// such as from in-memory PEM strings, or in-memory binary DER encoded form, etc.
let cert1 = chilkat::Cert::new();
success = cert1.load_from_file("recipient1.cer").is_ok();
if !success {
println!("{}", cert1.last_error_text());
return;
}
let cert2 = chilkat::Cert::new();
success = cert2.load_from_file("recipient2.cer").is_ok();
if !success {
println!("{}", cert2.last_error_text());
return;
}
let cert3 = chilkat::Cert::new();
success = cert3.load_from_file("recipient3.cer").is_ok();
if !success {
println!("{}", cert3.last_error_text());
return;
}
// Create a new email object
let email = chilkat::Email::new();
email.set_subject("This email is encrypted and sent to 3 recipients");
email.set_body("This is an S/MIME encrypted mail sent to 3 recipients");
email.set_from("Chilkat Support <support@chilkatsoft.com>");
// Make each of the certificates available for encrypting the email
// by calling AddEncryptCert for each.
success = email.add_encrypt_cert(&cert1).is_ok();
if success {
success = email.add_encrypt_cert(&cert2).is_ok();
}
if success {
success = email.add_encrypt_cert(&cert3).is_ok();
}
if !success {
println!("{}", email.last_error_text());
return;
}
// Add 3 recipients to the email (2 TO addresses, and 1 CC address)
success = email.add_to("Recipient 1", "admin@cknotes.com").is_ok();
success = email.add_to("Recipient 2", "somebody001122@yahoo.com").is_ok();
success = email.add_cc("Recipient 3", "somebody123xyz@gmail.com").is_ok();
// Indicate that the email is to be sent encrypted.
email.set_send_encrypted(true);
// Send the encrypted email...
if mailman.send_email(&email).is_err() {
println!("{}", mailman.last_error_text());
} else {
println!("Encrypted Email Sent!");
}