Verify DomainKey-Signature Headers in Downloaded Email
See more DKIM / DomainKey Examples
Downloads email from an IMAP server and verifies the DomainKey-Signature header(s) in each email, if present.
Note: DKIM-Signatures are much more common than DomainKey-Signatures. See the other Chilkat example for verifying DKIM-Signatures (link in the code below).
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.
let imap = chilkat::Imap::new();
// Connect to an IMAP server, login, select mailbox..
// Use TLS
imap.set_ssl(true);
imap.set_port(993);
success = imap.connect("imap.example.com").is_ok();
if success {
success = imap.login("myLogin", "myPassword").is_ok();
if success {
success = imap.select_mailbox("Inbox").is_ok();
}
}
if !success {
println!("{}", imap.last_error_text());
return;
}
// Note: DKIM-Signatures are much more common than DomainKey-Signature
// See DKIM-Signature Verify Sample.
let dkim = chilkat::Dkim::new();
// Download a max of 10 emails and verify any DomainKey-Signature headers
// that are present.
// Download emails by sequence numbers (not UIDs).
let b_uid = false;
let mut seq_num: i32 = 0;
let mut j: i32 = 0;
let mut n = imap.num_messages();
if n > 50 {
n = 50;
}
let json = chilkat::JsonObject::new();
json.set_emit_compact(false);
// To verify DomainKey-Signature headers, we need the exact unmodified MIME bytes of each email.
let mime_data = chilkat::BinData::new();
seq_num = 1;
while seq_num <= n {
// The FetchSingleBd method was introduced in v9.5.0.76
if imap.fetch_single_bd(seq_num as u32, b_uid, &mime_data).is_err() {
println!("{}", imap.last_error_text());
return;
}
// Note: DKIM-Signatures are much more common than DomainKey-Signature
// See DKIM-Signature Verify Sample.
// Get the number of DomainKey-Signature headers.
let num_sigs = dkim.num_domain_key_sigs(&mime_data);
// Verify each..
j = 0;
while j < num_sigs {
println!("------ DomainKey Signature {}", j);
if dkim.domain_key_verify(j, &mime_data).is_err() {
println!("Not valid.");
println!("{}", dkim.last_error_text());
} else {
println!("valid.");
}
// Show the additional information about the signature verification
let _ = json.load(&dkim.verify_info());
println!("{}", json.emit().unwrap_or_default());
// The JSON contains information such as this:
// {
// "domain": "amazonses.com",
// "selector": "7v7vs6w47njt4pimodk5mmttbegzsi6n",
// "publicKey": "MIGfMA0GCSqG...v2GvWPqGHz6uqeQIDAQAB",
// "canonicalization": "relaxed/simple",
// "algorithm": "rsa-sha256",
// "signedHeaders": "Subject:From:To:Date:Mime-Version:Content-Type:References:Message-Id:Feedback-ID",
// "verified": "yes"
// }
j = j + 1;
}
seq_num = seq_num + 1;
}
success = imap.disconnect().is_ok();