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

Secure IMAP over TLS/SSL

To communicate with an IMAP server over TLS/SSL, simply set the Ssl and Port properties. The remainder of your code is the same as with non-SSL.

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 TLS/SSL connection, set the Ssl property and the port:
imap.set_ssl(true);
// The typical port for IMAP SSL is 993
imap.set_port(993);

// 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();