Rust
Rust
Add Email Attachment from FTP
Downloads (into memory) a file from an FTP server and adds it as an attachment to an email.Note: This example requires Chilkat v9.5.0.63 or later.
Chilkat Rust Downloads
let mut success = false;
// Note: This example requires Chilkat v9.5.0.63 or later.
// This example assumes Chilkat Ftp2 to have been previously unlocked.
// See Unlock Ftp2 for sample code.
let ftp = chilkat::Ftp2::new();
ftp.set_hostname("www.my-ftp-server.com");
ftp.set_username("mFtpLogin");
ftp.set_password("myFtpPassword");
// Connect to the FTP server.
success = ftp.connect_only().is_ok();
if !success {
println!("{}", ftp.last_error_text());
return;
}
// Authenticate with the FTP server.
success = ftp.login_after_connect_only().is_ok();
if !success {
println!("{}", ftp.last_error_text());
return;
}
// Move to the remove directory where our file is located.
success = ftp.change_remote_dir("qa_data").is_ok();
if success {
success = ftp.change_remote_dir("pdf").is_ok();
}
if !success {
println!("{}", ftp.last_error_text());
return;
}
// Download...
let pdf_data = chilkat::BinData::new();
if ftp.get_file_bd("fishing.pdf", &pdf_data).is_err() {
println!("{}", ftp.last_error_text());
return;
}
let _ = ftp.disconnect();
// Create an email object, and add the PDF as an attachment.
let email = chilkat::Email::new();
email.set_subject("Test with PDF attachment.");
email.set_body("This is a plain-text body..");
// Add the PDF attachment. (This method call requires Chilkat v9.5.0.63 or later)
if email.add_attachment_bd("fishing.pdf", &pdf_data, "application/pdf").is_err() {
println!("{}", email.last_error_text());
return;
}
// Save the email and examine with a text editor to see the PDF attachment is present..
let _ = email.save_eml("qa_output/out.eml");
println!("Success.");