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

IMAP Download and Verify Signed (S/MIME) Email

See more IMAP Examples

Demonstrates how to download and verify digitally signed S/MIME email.

Chilkat Rust Downloads

Rust

// 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.
// Use TLS
imap.set_ssl(true);
imap.set_port(993);
if imap.connect("imap.example.com").is_err() {
    println!("{}", imap.last_error_text());
    return;
}

if imap.login("myLogin", "myPassword").is_err() {
    println!("{}", imap.last_error_text());
    return;
}

// Select an IMAP mailbox
if imap.select_mailbox("Inbox").is_err() {
    println!("{}", imap.last_error_text());
    return;
}

// We can choose to fetch UIDs or sequence numbers.
let fetch_uids = true;

// Get the message IDs of all the emails in the mailbox
let message_set = chilkat::MessageSet::new();
if imap.query_mbx("ALL", fetch_uids, &message_set).is_err() {
    println!("{}", imap.last_error_text());
    return;
}

let email = chilkat::Email::new();
let cert = chilkat::Cert::new();

let mut i = 0;
while i < message_set.count() {

    let uid = message_set.get_id(i);
    println!("uid: {}", uid);

    if imap.fetch_email(false, uid, true, &email).is_err() {
        println!("{}", imap.last_error_text());
        return;
    }

    // The security layers of signed and/or encrypted emails
    // are automatically "unwrapped" when loaded into
    // a Chilkat email object.
    // An application only needs to check to see if an email
    // was received signed or encrypted, and then examine
    // the success/failure.  For example:
    if email.received_signed() {

        println!("This email was signed.");

        // Check to see if the signatures were verified.
        if email.signatures_valid() {
            println!("Digital signature(s) verified.");
            println!("Signer: {}", email.signed_by());

            // Get the certificate used for signing.

            if email.last_signer_cert(0, &cert).is_err() {
                println!("Failed to get signing certificate object.");
            } else {
                println!("Signing cert: {}", cert.subject_cn());
            }

        } else {
            println!("Digital signature verification failed.");
        }

    }

    i = i + 1;
}

// Disconnect from the IMAP server.
let _ = imap.disconnect().is_ok();