Sample code for 30+ languages & platforms
Rust Requires Chilkat v11.0.0+

Sign an email and send the signed bytes over SMTP

See more DKIM Examples

Compose the email, render it to MIME once, add its DKIM signature, and submit those same bytes to an SMTP server. Put blind-copy recipients in the SMTP envelope only so their addresses stay out of the visible headers.

Use an authorized SMTP account and your own sender domain. Publish the selector’s matching public key first. If your relay rewrites signed content, have it apply the signature after its final edits. A successful SMTP submission means the relay accepted the message. A recipient can then verify it with the public key in DNS.

Chilkat Rust Downloads

Rust

// Assumes Chilkat is unlocked. See Global Unlock Sample.

let mail = chilkat::MailMan::new();
mail.set_smtp_host("smtp.example.com");
mail.set_smtp_port(465);
mail.set_smtp_ssl(true);
mail.set_require_ssl_cert_verify(true);
mail.set_smtp_username("SMTP_USERNAME");
mail.set_smtp_password("SMTP_PASSWORD");
let email = chilkat::Email::new();
email.set_from("Workshop team <updates@example.com>");
email.set_subject("Workshop registration");
email.set_body("Registration opens on Monday.");
if email.add_to("Newsletter editor", "editor@example.net").is_err() {
    println!("{}", email.last_error_text());
    return;
}

// Render after all headers, recipients, and content are final.
let message = chilkat::BinData::new();
if mail.render_to_mime_bd(&email, &message).is_err() {
    println!("{}", mail.last_error_text());
    return;
}

// Supply your RSA signing key. For an encrypted PEM, get its password from your credential store.
let private_key = chilkat::PrivateKey::new();
if private_key.load_any_format_file("dkim-private.pem", "").is_err() {
    println!("{}", private_key.last_error_text());
    return;
}

let signer = chilkat::Dkim::new();
signer.set_dkim_domain("example.com");
signer.set_dkim_selector("mail2026");
signer.set_dkim_alg("rsa-sha256");
if signer.set_dkim_private_key(&private_key).is_err() {
    println!("{}", signer.last_error_text());
    return;
}

if signer.dkim_sign(&message).is_err() {
    println!("{}", signer.last_error_text());
    return;
}

// archive@example.com is an envelope-only blind copy.
let recipients = "editor@example.net,archive@example.com".to_string();
if mail.send_mime_bd("updates@example.com", &recipients, &message).is_err() {
    println!("{}", mail.last_error_text());
    return;
}

println!("SMTP relay accepted the signed message.");
if mail.close_smtp_connection().is_err() {
    println!("The SMTP session did not close cleanly.");
}