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

Forward an Email using CreateForward

See more Email Object Examples

Reads an email from an IMAP server, creates a forward version of the email using the CreateForward method, and sends the email to another recipient.

Chilkat Rust Downloads

Rust

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

// Read the 1st (most recent) email in an Inbox.
let imap = chilkat::Imap::new();

// Connect to an IMAP server.
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;
}

let num_emails = imap.num_messages();

// Fetch the email at the last sequence number.
// (We are assuming the Inbox has at least 1 email)

let email = chilkat::Email::new();
if imap.fetch_email(false, num_emails as u32, false, &email).is_err() {
    println!("{}", imap.last_error_text());
    return;
}

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

println!("{}", email.subject());

let e_forward = chilkat::Email::new();
if email.to_forward(&e_forward).is_err() {
    println!("{}", email.last_error_text());
    return;
}

// The eForward email has no To or CC recipients yet.
// Add one or more..
let _ = e_forward.add_to("Joe", "joe@example.com");

// We also need to specify the From name/address.
e_forward.set_from_address("matt@someMailServer.com");
e_forward.set_from_name("Matt");

// If we wish to add text at the start of the email body:
let sb_html_body = chilkat::StringBuilder::new();
if e_forward.has_html_body() {
    let _ = sb_html_body.append(&e_forward.get_html_body().unwrap_or_default());
    let _ = sb_html_body.prepend("<p>Hello, this is an email I'm forwarding to you...</p>");
    e_forward.set_html_body(&sb_html_body.get_as_string().unwrap_or_default());
}

let sb_pt_body = chilkat::StringBuilder::new();
if e_forward.has_plain_text_body() {
    let _ = sb_pt_body.append(&e_forward.get_plain_text_body().unwrap_or_default());
    let _ = sb_pt_body.prepend("Hello, this is an email I'm forwarding to you...\r\n\r\n");
    e_forward.set_text_body(&sb_pt_body.get_as_string().unwrap_or_default(), "text/plain");
}

// We could save the .eml, then double-click on it to view in our mail program, such as Outlook or Thunderbird..
let _ = e_forward.save_eml("qa_output/forward.eml");

// We could send (forward) the email..
let mailman = chilkat::MailMan::new();

mailman.set_smtp_host("smtp.example.com");
mailman.set_smtp_username("myLogin");
mailman.set_smtp_password("myPassword");
mailman.set_smtp_port(587);
mailman.set_start_tls(true);

if mailman.send_email(&e_forward).is_err() {
    println!("{}", mailman.last_error_text());
    return;
}

if mailman.close_smtp_connection().is_err() {
    println!("Connection to SMTP server not closed cleanly.");
}

println!("Mail Sent!");