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

IMAP using SOCKS5, SOCKS4 Proxy

Demonstrates how to connect to an IMAP server through a SOCKS5 or SOCKS4 proxy.

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 SOCKS4 or SOCKS5 proxy, simply set the following
// properties prior to connecting with the IMAP server:
// The SOCKS hostname may be a domain name or 
// IP address:
imap.set_socks_hostname("www.mysocksproxyserver.com");
imap.set_socks_port(1080);
imap.set_socks_username("myProxyLogin");
imap.set_socks_password("myProxyPassword");
// Set the SOCKS version to 4 or 5 based on the version
// of the SOCKS proxy server:
imap.set_socks_version(5);
// Note: SOCKS4 servers only support usernames without passwords.
// SOCKS5 servers support full login/password authentication.

// Connect to an IMAP server through the SOCKS proxy.
// Use TLS
imap.set_ssl(true);
imap.set_port(993);
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
// We can choose to fetch UIDs or sequence numbers.
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();