Rust Requires Chilkat v11.0.0+
Rust
Forward by Attaching the Existing Email to a New Email
See more Email Object Examples
Demonstrates how to forward an email by attaching the email to a new email.This example reads an email from an IMAP server, attaches the email to a new email, and sends the new email.
Chilkat Rust Downloads
// 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());
// Create a new email. The email we just read will be attached to this email.
let e_forward = chilkat::Email::new();
let _ = e_forward.add_to("Joe", "joe@example.com");
e_forward.set_from_address("matt@somewhere.com");
e_forward.set_from_name("Matt");
e_forward.set_subject("This is an email with another email attached.");
e_forward.set_html_body("<p>Hello, this is an email I'm forwarding to you. See the attached email.</p>");
// Attach the email.
let _ = e_forward.attach_email(&email);
// 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!");