Rust
Rust
Verify DKIM-Signature Headers in Downloaded Email
See more DKIM / DomainKey Examples
Downloads email from an IMAP server and verifies the DKIM-Signature header(s) in each email, if present.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;
}
let dkim = chilkat::Dkim::new();
// Download a max of 10 emails and verify any DKIM-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 > 10 {
n = 10;
}
let json = chilkat::JsonObject::new();
json.set_emit_compact(false);
// To verify DKIM-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;
}
// Get the number of DKIM-Signature headers.
let num_dkim = dkim.num_dkim_sigs(&mime_data);
// Verify each..
j = 0;
while j < num_dkim {
println!("------ DKIM Signature {}", j);
if dkim.dkim_verify(j, &mime_data).is_err() {
println!("Not valid.");
} 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();