Rust
Rust
Copy Email from one IMAP Account to Another
See more IMAP Examples
Demonstrates how to copy the email in a mailbox from one account to another.Chilkat Rust Downloads
let imap_src = chilkat::Imap::new();
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// Connect to our source IMAP server.
imap_src.set_ssl(true);
imap_src.set_port(993);
if imap_src.connect("MY-IMAP-DOMAIN").is_err() {
println!("{}", imap_src.last_error_text());
return;
}
// Login to the source IMAP server
if imap_src.login("MY-IMAP-LOGIN", "MY-IMAP-PASSWORD").is_err() {
println!("{}", imap_src.last_error_text());
return;
}
let imap_dest = chilkat::Imap::new();
// Connect to our destination IMAP server.
imap_dest.set_ssl(true);
imap_dest.set_port(993);
if imap_dest.connect("MY-IMAP-DOMAIN2").is_err() {
println!("{}", imap_dest.last_error_text());
return;
}
// Login to the destination IMAP server
if imap_dest.login("MY-IMAP-LOGIN2", "MY-IMAP-PASSWORD2").is_err() {
println!("{}", imap_dest.last_error_text());
return;
}
// Select a source IMAP mailbox on the source IMAP server
if imap_src.select_mailbox("Inbox").is_err() {
println!("{}", imap_src.last_error_text());
return;
}
let fetch_uids = true;
// Get the set of UIDs for all emails on the source server.
let Ok(mset) = imap_src.search("ALL", fetch_uids) else {
println!("{}", imap_src.last_error_text());
return;
};
// Load the complete set of UIDs that were previously copied.
// We dont' want to copy any of these to the destination.
let fac = chilkat::FileAccess::new();
let mset_already_copied = chilkat::MessageSet::new();
let mut str_msg_set = fac.read_entire_text_file("qa_cache/saAlreadyLoaded.txt", "utf-8").unwrap_or_default();
if fac.last_method_success() {
let _ = mset_already_copied.from_compact_string(&str_msg_set);
}
let num_uids = mset.count();
let sb_flags = chilkat::StringBuilder::new();
let mut i = 0;
while i < num_uids {
// If this UID was not already copied...
let uid = mset.get_id(i) as i32;
if !mset_already_copied.contains_id(uid as u32) {
println!("copying {}...", uid);
// Get the flags.
let Ok(flags) = imap_src.fetch_flags(uid as u32, true) else {
println!("{}", imap_src.last_error_text());
return;
};
let _ = sb_flags.set_string(&flags);
// Get the MIME of this email from the source.
let Ok(mime_str) = imap_src.fetch_single_as_mime(uid as u32, true) else {
println!("{}", imap_src.last_error_text());
return;
};
let seen = sb_flags.contains("\\Seen", false);
let flagged = sb_flags.contains("\\Flagged", false);
let answered = sb_flags.contains("\\Answered", false);
let draft = sb_flags.contains("\\Draft", false);
if imap_dest.append_mime_with_flags("Inbox", &mime_str, seen, flagged, answered, draft).is_err() {
println!("{}", imap_dest.last_error_text());
return;
}
// Update msetAlreadyCopied with the uid just copied.
mset_already_copied.insert_id(uid as u32);
// Save at every iteration just in case there's a failure..
str_msg_set = mset_already_copied.to_compact_string().unwrap_or_default();
let _ = fac.write_entire_text_file("qa_cache/saAlreadyLoaded.txt", &str_msg_set, "utf-8", false);
}
i = i + 1;
}
// Disconnect from the IMAP servers.
let _ = imap_src.disconnect().is_ok();
let _ = imap_dest.disconnect().is_ok();