Rust Requires Chilkat v11.0.0+
Rust
Fetch 1st N Headers of Search Results
Calls Search to get a message set, then downloads the 1st N messages in the message set. There are two equivalent ways of doing it: (1) iterate from 0 to N-1 and download each message individually, or (2) create a new message set that contains the 1st N messages and pass it to FetchHeaders. Both are demonstrated here.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("****", "****").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;
}
let num_found = message_set.count();
if num_found == 0 {
println!("No messages found.");
return;
}
// Get the 1st 10 messages in messageSet.
let mut upper_bound = 10;
if num_found < upper_bound {
upper_bound = num_found;
}
let mut i = 0;
let b_uid = message_set.has_uids();
let header_only = true;
let email = chilkat::Email::new();
while i < upper_bound {
if imap.fetch_email(header_only, message_set.get_id(i), b_uid, &email).is_err() {
println!("{}", imap.last_error_text());
return;
}
println!("{}: {}", i, email.subject());
i = i + 1;
}
// Disconnect from the IMAP server.
let _ = imap.disconnect().is_ok();