Rust Requires Chilkat v11.0.0+
Rust
Extract signed invoice XML from an Italian PEC message
See more Email Object Examples
Search the PEC folder by subject, fetch one envelope by UID, and inspect the attachments of each nested email. Match .xml.p7m by filename and verify the opaque CMS signature before loading the XML. This checks signature integrity. Certificate trust and invoice schema validation are separate application decisions.
Chilkat Rust Downloads
// 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);
if imap.connect("imap.example.com").is_err() {
println!("{}", imap.last_error_text());
return;
}
if imap.login("IMAP_USERNAME", "IMAP_PASSWORD").is_err() {
println!("{}", imap.last_error_text());
return;
}
if imap.select_mailbox("INBOX/postacert").is_err() {
println!("{}", imap.last_error_text());
return;
}
let messages = chilkat::MessageSet::new();
if imap.query_mbx("SUBJECT \"POSTA CERTIFICATA: Invio File\"", true, &messages).is_err() {
println!("{}", imap.last_error_text());
return;
}
if messages.count() == 0 {
println!("No matching PEC envelope.");
let _ = imap.disconnect();
return;
}
let envelope = chilkat::Email::new();
if imap.fetch_email(false, messages.get_id(0), true, &envelope).is_err() {
println!("{}", imap.last_error_text());
return;
}
let _ = imap.disconnect();
let nested = chilkat::Email::new();
let name = chilkat::StringBuilder::new();
let mut verified = 0;
let mut i = 0;
while i < envelope.num_attached_messages() {
if envelope.get_attached_email(i, &nested).is_err() {
println!("{}", envelope.last_error_text());
return;
}
let mut j = 0;
while j < nested.num_attachments() {
let _ = name.set_string(&nested.get_attachment_filename(j).unwrap_or_default());
if name.ends_with(".xml.p7m", false) {
let data = chilkat::BinData::new();
if nested.get_attachment_bd(j, &data).is_err() {
println!("{}", nested.last_error_text());
return;
}
let cms = chilkat::Crypt2::new();
if cms.opaque_verify_bd(&data).is_err() {
println!("{}", cms.last_error_text());
return;
}
let invoice = chilkat::Xml::new();
if invoice.load_bd(&data, true).is_err() {
println!("{}", invoice.last_error_text());
return;
}
println!("{} XML root: {}", name.get_as_string().unwrap_or_default(), invoice.tag());
verified = verified + 1;
}
j = j + 1;
}
i = i + 1;
}
println!("Verified XML invoices: {}", verified);