Rust Requires Chilkat v11.0.0+
Rust
Copy an Email from One Mailbox to Another
Copies an email from one IMAP folder to another. After running this example, copies of the email will be present in both source and destination folders.Chilkat Rust Downloads
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// This example copies an email from one mailbox to another.
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("***", "***").is_err() {
println!("{}", imap.last_error_text());
return;
}
// Select an IMAP mailbox
if imap.select_mailbox("Inbox.testing.a").is_err() {
println!("{}", imap.last_error_text());
return;
}
let fetch_uids = true;
// Get the message IDs for all emails having "Re:" in the subject.
let message_set = chilkat::MessageSet::new();
if imap.query_mbx("SUBJECT Re:", fetch_uids, &message_set).is_err() {
println!("{}", imap.last_error_text());
return;
}
// Copy the messages from "Inbox.testing.a" to "Inbox.testing.b" in one call to CopyMultiple:
if imap.copy_multiple(&message_set, "Inbox.testing.b").is_err() {
println!("{}", imap.last_error_text());
return;
}
// Alternatively, loop over each message in the set and
// copy each separately:
let mut i = 0;
while i < message_set.count() {
let msg_id = message_set.get_id(i) as i32;
let is_uid = message_set.has_uids();
if imap.copy(msg_id as u32, is_uid, "Inbox.testing.c").is_err() {
println!("{}", imap.last_error_text());
return;
}
i = i + 1;
}
// Display the session log.
println!("{}", imap.session_log());
// Disconnect from the IMAP server.
let _ = imap.disconnect().is_ok();