Rust Requires Chilkat v11.0.0+
Rust
Download Unread Email from IMAP Mailbox
Download unread email messages from an IMAP mailbox.Chilkat Rust Downloads
// This example assumes 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;
}
// Login
if imap.login("admin@chilkatsoft.com", "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;
}
// Find emails marked as seen or not already seen:
let seen_search = "SEEN".to_string();
let not_seen_search = "NOT SEEN".to_string();
// Get the set of unseen message UIDs
let fetch_uids = true;
let message_set = chilkat::MessageSet::new();
if imap.query_mbx(¬_seen_search, fetch_uids, &message_set).is_err() {
println!("{}", imap.last_error_text());
return;
}
// Fetch the unseen 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;
}
// Display the Subject and From of each email.
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.get_header_field("Date").unwrap_or_default());
println!("{}", email.subject());
println!("{}", email.from());
println!("--");
i = i + 1;
}
// Disconnect from the IMAP server.
let _ = imap.disconnect().is_ok();