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

Using Client Certificate w/ IMAP SSL

Demonstrates how to use a client-side certificate with an IMAP SSL connection. The SetSslClientCert method is called to specify a certificate to be used for the SSL connection.

Chilkat Rust Downloads

Rust

// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

let imap = chilkat::Imap::new();

// To use a secure SSL connection, set SSL and the port:
imap.set_ssl(true);
// The typical port for IMAP SSL is 993
imap.set_port(993);

// Load a certificate from a PFX file and use it.
// Note: Other methods are available to load pre-installed
// certificates from registry-based certificate stores.

// Create an instance of a certificate store object, load a PFX file,
// locate the certificate we need, and use it for signing.
// (a PFX file may contain more than one certificate.)
let cert_store = chilkat::CertStore::new();
// The 1st argument is the filename, the 2nd arg is the 
// PFX file's password:
if cert_store.load_pfx_file("myCertWithPrivateKey.pfx", "secret").is_err() {
    println!("{}", cert_store.last_error_text());
    return;
}

// Find the certificate by the subject common name:
let json_cn = chilkat::JsonObject::new();
let _ = json_cn.update_string("CN", "cert common name");

let cert = chilkat::Cert::new();
if cert_store.find_cert(&json_cn, &cert).is_err() {
    println!("{}", cert_store.last_error_text());
    return;
}

// If a PFX file is known to contain a single certificate,
// you may load it directly into a Chilkat certificate object.
// This snippet of source code shows how:
let cert2 = chilkat::Cert::new();
// The 1st argument is the filename, the 2nd arg is the 
// PFX file's password:
if cert2.load_pfx_file("myClientCert.pfx", "secret").is_err() {
    println!("{}", cert.last_error_text());
    return;
}

// Use the cert:
let _ = imap.set_ssl_client_cert(&cert).is_ok();

// Connect to an IMAP server.
if imap.connect("imap.example.com").is_err() {
    println!("{}", imap.last_error_text());
    return;
}

// Login
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;
}

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

// Fetch the emails into a bundle object:
let bundle = chilkat::EmailBundle::new();
let headers_only = false;
if imap.fetch_msg_set(headers_only, &message_set, &bundle).is_err() {
    println!("{}", imap.last_error_text());
    return;
}

// Loop over the bundle and display the FROM and SUBJECT of each.
let email = chilkat::Email::new();
let mut i = 0;
let num_emails = bundle.message_count();
while i < num_emails {
    let _ = bundle.email_at(i, &email);

    println!("{}", email.from());
    println!("{}", email.subject());
    println!("--");
    i = i + 1;
}

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