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

Verify each DKIM signature in email downloaded over IMAP

See more DKIM Examples

Download the first ten messages (or all messages in a smaller inbox) as complete MIME, including attachments, while preserving their unread flags. The verifier looks up the selector’s TXT record when it needs a key. Inspect VerifyInfo immediately after each attempt for the signing domain, selector, and any body-hash mismatch.

Verify each signature independently, in top-to-bottom order. A passing signature authenticates the signing domain’s responsibility for the signed content. Identifying the author and checking DMARC alignment with the visible From address are separate steps. A missing DNS key or unavailable resolver can also prevent verification. DKIM specification.

Chilkat Rust Downloads

Rust
let mut success = false;

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

let imap = chilkat::Imap::new();
imap.set_ssl(true);
imap.set_port(993);
imap.set_require_ssl_cert_verify(true);
imap.set_peek_mode(true);
imap.set_auto_download_attachments(true);
success = imap.connect("imap.example.com").is_ok();
if !success {
    println!("{}", imap.last_error_text());
    return;
}

success = imap.login("IMAP_USERNAME", "IMAP_PASSWORD").is_ok();
if !success {
    println!("{}", imap.last_error_text());
    return;
}

success = imap.select_mailbox("INBOX").is_ok();
if !success {
    println!("{}", imap.last_error_text());
    return;
}

// Select the first ten sequence positions, then fetch by stable UID.
let messages = chilkat::MessageSet::new();
success = imap.query_mbx("1:10", true, &messages).is_ok();
if !success {
    println!("{}", imap.last_error_text());
    return;
}

let mut i = 0;
let verifier = chilkat::Dkim::new();
let message = chilkat::BinData::new();
while i < messages.count() {
    let uid = messages.get_id(i);
    println!("Message UID {}", uid);
    success = imap.fetch_single_bd(uid, true, &message).is_ok();
    if !success {
        println!("{}", imap.last_error_text());
    } else {
        let count = verifier.num_dkim_sigs(&message);
        if count == 0 {
            println!("No DKIM signatures.");
        }

        let mut sig = 0;
        while sig < count {
            success = verifier.dkim_verify(sig, &message).is_ok();
            // Read diagnostics before the next verification replaces them.
            let info = chilkat::JsonObject::new();
            let have_info = info.load(&verifier.verify_info()).is_ok();
            if success {
                println!("Signature {} verified.", sig);
            } else {
                if (have_info) && (info.string_of_equals("bodyHashVerified", "no", true)) {
                    println!("Signature {}: the body hash differs.", sig);
                } else {
                    println!("Signature {} could not be verified.", sig);
                }

                println!("{}", verifier.last_error_text());
            }

            if (have_info) && (info.json_type_of("domain") == 1) {
                println!("Signing domain: {}  Selector: {}", info.string_of("domain").unwrap_or_default(), info.string_of("selector").unwrap_or_default());
            }

            sig = sig + 1;
        }

    }

    i = i + 1;
}

success = imap.disconnect().is_ok();