Rust Requires Chilkat v11.0.0+
Rust
IMAP Delete Old Email (before a specified date)
See more IMAP Examples
Demonstrates how to delete email older than a particular date.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);
// Use your IMAP server domain. This example
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 message ID's for emails older than 1/1/2025
let fetch_uids = true;
let message_set = chilkat::MessageSet::new();
if imap.query_mbx("SENTBEFORE 01-Jan-2025", fetch_uids, &message_set).is_err() {
println!("{}", imap.last_error_text());
return;
}
// If desired, we can examine the Subject of each email to be deleted..
let email = chilkat::Email::new();
let mut i = 0;
let count = message_set.count();
let header_only = true;
while i < count {
if imap.fetch_email(header_only, message_set.get_id(i), true, &email).is_err() {
println!("{}", imap.last_error_text());
return;
}
println!("{}", email.local_date_str());
println!("{}", email.subject());
i = i + 1;
}
// Set the Deleted flag for each message:
if imap.set_flags(&message_set, "Deleted", 1).is_err() {
println!("{}", imap.last_error_text());
return;
}
// Expunge and close the mailbox.
if imap.expunge_and_close().is_err() {
println!("{}", imap.last_error_text());
return;
}
// Disconnect from the IMAP server.
let _ = imap.disconnect().is_ok();