Rust Requires Chilkat v11.0.0+
Rust
Get IMAP UID from Email Header
See more IMAP Examples
Demonstrates how to get the IMAP UID from an email header. After fetching an email or header using IMAP, the UID is contained in the ckx-imap-uid header field.Chilkat Rust Downloads
// This example requires 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("login", "password").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;
}
let fetch_uids = true;
// Get the message UIDs of all the emails in the mailbox
let message_set = chilkat::MessageSet::new();
if imap.query_mbx("ALL", fetch_uids, &message_set).is_err() {
println!("{}", imap.last_error_text());
return;
}
let bundle = chilkat::EmailBundle::new();
let headers_only = true;
if imap.fetch_msg_set(headers_only, &message_set, &bundle).is_err() {
println!("{}", imap.last_error_text());
return;
}
// The UID of each fetched email header is available
// in the ckx-imap-uid header field.
let email = chilkat::Email::new();
let msg_set1 = chilkat::MessageSet::new();
let mut i = 0;
let sz_bundle = bundle.message_count();
while i < sz_bundle {
let _ = bundle.email_at(i, &email);
// Build a message set containing one UID
let uid_str = email.get_header_field("ckx-imap-uid").unwrap_or_default();
let _ = msg_set1.from_compact_string(&uid_str);
// Move this message to some other folder.
if imap.move_messages(&msg_set1, "someOtherFolder").is_err() {
println!("{}", imap.last_error_text());
// Exit the loop...
i = sz_bundle;
}
i = i + 1;
}
// Disconnect from the IMAP server.
let _ = imap.disconnect().is_ok();