Rust Requires Chilkat v11.0.0+
Rust
Mark IMAP Email as Read/Unread (Seen/Unseen)
Demonstrates how to mark emails as read or unread.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("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;
}
// Set PeekMode so that downloaded messages are not
// automatically marked as seen.
imap.set_peek_mode(true);
// The NumMessages property contains the number of messages
// in the currently selected mailbox.
let num_msgs = imap.num_messages();
if num_msgs == 0 {
return;
}
let email = chilkat::Email::new();
for i in 1..=num_msgs {
// Download each email by sequence number (not UID)
if imap.fetch_email(false, i as u32, false, &email).is_err() {
println!("{}", imap.last_error_text());
return;
}
// If desired, mark the email as SEEN. There are two
// ways to do it:
// 1) Set the flag directly by using the sequence number
// Indicate that we are passing a sequence number and
// not a UID:
let b_is_uid = false;
// Set the SEEN flag = 1 to mark the email as SEEN,
// or set it to 0 to mark it as not-seen.
if imap.set_flag(i as u32, b_is_uid, "SEEN", 1).is_err() {
println!("{}", imap.last_error_text());
return;
}
// 2) Alternatively, we can use the email object.
// When an email is downloaded from the IMAP server
// Chilkat will add a "ckx-imap-uid" header to the email.
// This makes it possible to know the UID associated with
// the email. (This is not the sequence number, which may change
// from session to session, but the UID which does not change.
// The SetMailFlag method is identical to SetFlag, except
// it gets the UID from the ckx-imap-uid header.
// For example:
if imap.set_mail_flag(&email, "SEEN", 1).is_err() {
println!("{}", imap.last_error_text());
return;
}
}
// Disconnect from the IMAP server.
let _ = imap.disconnect().is_ok();