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

How to Copy IMAP Mail to another IMAP Server

Demonstrates how to copy the entire contents of an IMAP mailbox to another IMAP server.

Chilkat Rust Downloads

Rust

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

let imap_src = chilkat::Imap::new();

// Connect to our source IMAP server.
if imap_src.connect("imap.example.com").is_err() {
    println!("{}", imap_src.last_error_text());
    return;
}

// Login to the source IMAP server
if imap_src.login("admin@chilkatsoft.com", "myPassword").is_err() {
    println!("{}", imap_src.last_error_text());
    return;
}

let imap_dest = chilkat::Imap::new();

// Connect to our destination IMAP server.
if imap_dest.connect("mail.example-code.com").is_err() {
    println!("{}", imap_dest.last_error_text());
    return;
}

// Login to the destination IMAP server
if imap_dest.login("myLogin", "myPassword").is_err() {
    println!("{}", imap_dest.last_error_text());
    return;
}

// Select an IMAP mailbox on the source IMAP server
if imap_src.select_mailbox("Inbox").is_err() {
    println!("{}", imap_src.last_error_text());
    return;
}

// After selecting a mailbox, the NumMessages property will
// be updated to reflect the total number of emails in the mailbox:
println!("{}", imap_src.num_messages());

// The emails in the mailbox will always have sequence numbers
// ranging from 1 to NumMessages.

// This example will copy the first 10 messages using sequence numbers
let sb_mime = chilkat::StringBuilder::new();
for seq_num in 1..=10 {
    sb_mime.clear();
    if imap_src.fetch_single_as_mime_sb(seq_num as u32, false, &sb_mime).is_err() {
        println!("{}", imap_src.last_error_text());
        return;
    }

    if imap_dest.append_mime_with_flags_sb("Inbox", &sb_mime, false, false, false, false).is_err() {
        println!("{}", imap_dest.last_error_text());
        return;
    }

}

// Disconnect from the IMAP servers.
let _ = imap_src.disconnect().is_ok();
let _ = imap_dest.disconnect().is_ok();