Sample code for 30+ languages & platforms
Rust Requires Chilkat v11.0.0+

Delete IMAP Email

To delete an email, the "Deleted" flag must be set to 1 for each message to be deleted. You must also call Expunge or ExpungeAndClose to remove the emails marked as deleted.

Chilkat Rust Downloads

Rust

// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

// This example deletes email matching a criteria from Inbox.
let imap = chilkat::Imap::new();

// Turn on session logging:
imap.set_keep_session_log(true);

// 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;
}

let fetch_uids = true;
// Get the message IDs for all emails having "FTP2" in the subject.
let message_set = chilkat::MessageSet::new();
if imap.query_mbx("SUBJECT FTP2", fetch_uids, &message_set).is_err() {
    println!("{}", imap.last_error_text());
    return;
}

// 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;
}

// Display the session log.
println!("{}", imap.session_log());

// Disconnect from the IMAP server.
let _ = imap.disconnect().is_ok();