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

Search IMAP Mailbox for Email Matching Criteria

Searching an IMAP mailbox for messages that match search criteria.

Chilkat Rust Downloads

Rust

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

// We can choose to fetch UIDs or sequence numbers.
let fetch_uids = true;

// Here are examples of different search criteria:

// Return all messages.
let all_msgs = "ALL".to_string();

// Search for already-answered emails.
let answered = "ANSWERED".to_string();

// Search for messages on a specific date.
// The date string is DD-Month-YYYY where Month is
// Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, or Dec.
let on_date = "SENTON 05-Mar-2007".to_string();

// Search for messages between two dates.  SENTBEFORE
// finds emails sent before a date, and SENTSINCE finds
// email sent on or after a date.  The "AND" operation
// is implied by joining criteria, separated by spaces.
let between_dates = "SENTSINCE 01-Mar-2007 SENTBEFORE 05-Mar-2007".to_string();

// Another example of AND: find all unanswered emails
// sent after 04-Mar-2007 with "Problem" in the subject:
let complex_search1 = "UNANSWERED SENTSINCE 04-Mar-2007 Subject \"Problem\"".to_string();

// Find messages with a specific string in the body:
let body_search = "BODY \"problem solved\"".to_string();

// Using OR.  The syntax is OR <criteria1> <criteria2>. 
// The "OR" comes first, followed by each criteria.
// For example, to match all emails with "Help" or "Question" in the subject.
// You'll notice that literal strings may be quoted or unquoted.
// If a literal contains SPACE characters, quote it:
let or_search = "OR SUBJECT Help SUBJECT Question".to_string();

// ----------------------------------------------
// Strings are case-insensitive when searching....
// ----------------------------------------------

// Find all emails sent from yahoo.com addresses:
let from_search = "FROM yahoo.com".to_string();
// Find all emails sent from anyone with "John" in their name:
let john_search = "FROM John".to_string();

// Find emails with the RECENT flag set:
let recent_search = "RECENT".to_string();

// Find emails that don't have the recent flag set:
let not_recent_search = "NOT RECENT".to_string();
// This is synonymous with "OLD":
let old_search = "OLD".to_string();

// Find all emails marked for deletion:
let marked_for_delete_search = "DELETED".to_string();

// Find all emails having a specified header field with a value
// containing a substring:
let header_search = "HEADER DomainKey-Signature paypal.com".to_string();

// Find any emails having a specific header field.  If the 
// 2nd argument to the "HEADER" criteria is an empty string,
// any email having the header field is returned regardless
// of the header field's content.
// Find any emails with a DomainKey-Signature field:
let header_exists_search = "HEADER DomainKey-Signature \"\"".to_string();

// Find NEW emails: these are emails that have the RECENT flag
// set, but not the SEEN flag:
let new_search = "NEW".to_string();

// Find emails larger than a certain number of bytes:
let size_larger_search = "LARGER 500000".to_string();

// Find emails marked as seen or not already seen:
let seen_search = "SEEN".to_string();
let not_seen_search = "NOT SEEN".to_string();

// Find emails having a given substring in the TO header field:
let to_search = "TO support@chilkatsoft.com".to_string();
// A more long-winded way to do the same thing:
let to_search2 = "HEADER TO support@chilkatsoft.com".to_string();

// Find emails smaller than a size in bytes:
let smaller_search = "SMALLER 30000".to_string();

// Find emails that have a substring anywhere in the header
// or body:
let full_substring_search = "TEXT \"Zip Component\"".to_string();

// Pass any of the above strings here to test a search:
let message_set = chilkat::MessageSet::new();
if imap.query_mbx(&or_search, fetch_uids, &message_set).is_err() {
    println!("{}", imap.last_error_text());
    return;
}

// Fetch the email headers into a bundle object:
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;
}

// Display the Subject and From of each email.
let email = chilkat::Email::new();
let mut i = 0;
while i < bundle.message_count() {
    let _ = bundle.email_at(i, &email);

    println!("{}", email.get_header_field("Date").unwrap_or_default());
    println!("{}", email.subject());
    println!("{}", email.from());
    println!("--");

    i = i + 1;
}

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