Rust
Rust
Send Encrypted Email using Certificate in Apple Keychain
See more Apple Keychain Examples
Send encrypted (S/MIME) email using a certificate found in the Apple Keychain.Note: This example requires Chilkat v10.1.2 or later.
Chilkat Rust Downloads
let email = chilkat::Email::new();
email.set_subject("This email is encrypted");
email.set_body("This is a S/MIME 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("", &recipient_email_addr);
// Indicate that the email is to be sent encrypted.
email.set_send_encrypted(true);
let cert = chilkat::Cert::new();
// -----------------------------------------------
// This example requires Chilkat v10.1.2 or later.
// -----------------------------------------------
// The recipient's certificate is used to encrypt.
// This will load the certificate from the Apple Keychain.
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();
email.set_send_encrypted(true);
let mailman = chilkat::MailMan::new();
mailman.set_smtp_host("smtp.example.com");
mailman.set_smtp_username("joe@example.com");
mailman.set_smtp_port(587);
mailman.set_start_tls(true);
// We'll get the SMTP password from the Apple Keychain.
// See how we originally saved the email credentials to the Keychain here:
// Save Email Credentials in Apple Keychain or Windows Credentials Manager
let secrets = chilkat::Secrets::new();
// On Windows, this is the Windows Credentials Manager
// On MacOS/iOS, it is the Apple Keychain
secrets.set_location("local_manager");
// Specify the name of the secret.
// service and username are required.
// appName and domain are optional.
// Note: The values are arbitrary and can be anything you want.
let json = chilkat::JsonObject::new();
let _ = json.update_string("appName", "MyEmailApp");
let _ = json.update_string("service", "SMTP");
let _ = json.update_string("domain", "example.com");
let _ = json.update_string("username", "joe@example.com");
mailman.set_smtp_password(&secrets.get_secret_str(&json).unwrap_or_default());
// Send the encrypted email.
// The email is encrypted and sent within the SendEmail function.
if mailman.send_email(&email).is_err() {
println!("{}", mailman.last_error_text());
return;
}
println!("Encrypted email sent!");