Rust
Rust
Send Email With Attachments
See more SMTP Examples
Demonstrates how to send an email with attachments.Chilkat Rust Downloads
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// The mailman object is used for sending (SMTP) and receiving (POP3) email.
let mailman = chilkat::MailMan::new();
// Set the SMTP server.
mailman.set_smtp_host("smtp.my-tls-mail-server.com");
// Set the SMTP login/password (if required)
mailman.set_smtp_username("MY_SMTP_USERNAME");
mailman.set_smtp_password("MY_SMTP_PASSWORD");
// Connect to SMTP port 465 using TLS.
mailman.set_smtp_ssl(true);
mailman.set_smtp_port(465);
// Create a new email object
let email = chilkat::Email::new();
email.set_subject("This is a test");
email.set_body("This is a test");
email.set_from("Chilkat Support <support@chilkatsoft.com>");
let _ = email.add_to("Chilkat Admin", "admin@chilkatsoft.com").is_ok();
// To add more recipients, call AddTo, AddCC, or AddBcc once per recipient.
// Add some attachments.
// The AddFileAttachment method returns the value of the content-type it chose for the attachment.
let Ok(mut content_type) = email.add_file_attachment("qa_data/jpg/starfish.jpg") else {
println!("{}", email.last_error_text());
return;
};
content_type = email.add_file_attachment("qa_data/pdf/fishing.pdf").unwrap_or_default();
if !email.last_method_success() {
println!("{}", email.last_error_text());
return;
}
// Call SendEmail to connect to the SMTP server and send.
// The connection (i.e. session) to the SMTP server remains
// open so that subsequent SendEmail calls may use the
// same connection.
if mailman.send_email(&email).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 with attachments sent!");