Rust Requires Chilkat v11.0.0+
Rust
Process New Email by Scanning for Senders
Scan email and save application-selected emails to EML files with unique filenames.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;
}
// We can choose to fetch UIDs or sequence numbers.
let fetch_uids = true;
// Fetch messages from the mailbox using a search criteria.
// This example finds NEW emails: these are emails that have the RECENT flag set, but not the SEEN flag:
let message_set = chilkat::MessageSet::new();
if imap.query_mbx("NEW", fetch_uids, &message_set).is_err() {
println!("{}", imap.last_error_text());
return;
}
// This example will download headers, and then download
// the full email for those emails sent from a contact
// in our database.
// When downloading headers, each email object contains
// (obviously) the headers, but the body will be missing.
// Also, attachments will not be included. However, it is
// possible to get information about the attachments
// as well as the complete size of the email.
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;
}
// Loop over the email objects...
let email_header = chilkat::Email::new();
let full_email = chilkat::Email::new();
let mut i = 0;
let num_emails = bundle.message_count();
while i < num_emails {
let _ = bundle.email_at(i, &email_header);
// The sender's email address and name are available
// in the From, FromAddress, and FromName properties.
// If the sender is "Chilkat Support <support@chilkatsoft.com",
// then the From property will hold the entire string.
// the FromName property contains"Chilkat Support",
// and the FromAddress property contains "support@chilkatsoft.com";
println!("{}", email_header.from());
println!("{}", email_header.from_address());
println!("{}", email_header.from_name());
// Assume at this point your code checks to see if the sender
// is one in your contacts database. If so, this is
// the code you would write to download the entire
// email and save it to a file.
// The ckx-imap-uid header field is added when
// headers are downloaded. This makes it possible
// to get the UID from the email object.
let uid_str = email_header.get_header_field("ckx-imap-uid").unwrap_or_default();
let uid = uid_str.parse::<i32>().unwrap_or(0);
if imap.fetch_email(false, uid as u32, true, &full_email).is_err() {
println!("{}", imap.last_error_text());
return;
}
// You can use the GenerateFilename method to
// generate a unique filename...
let filename = full_email.generate_filename().unwrap_or_default();
// SaveEml saves the entire email, including attachments.
let _ = full_email.save_eml(&filename).is_ok();
println!("--");
i = i + 1;
}
// Disconnect from the IMAP server.
let _ = imap.disconnect().is_ok();