Last active
September 1, 2024 02:57
-
-
Save zwpaper/bca2917433c56c4b541397f4a6ac6516 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use lettre::transport::smtp::SmtpTransport; | |
use lettre::{ | |
message::Message, | |
transport::smtp::{ | |
authentication::Credentials, | |
client::{Certificate, Tls, TlsParameters}, | |
}, | |
Transport, | |
}; | |
fn main() { | |
// Create the email message | |
let email = Message::builder() | |
.from("[email protected]".parse().unwrap()) | |
.to("[email protected]".parse().unwrap()) | |
.subject("Hello from Rust!") | |
.body(String::from( | |
"This is a test email sent using Rust with STARTTLS.", | |
)) | |
.unwrap(); | |
let cert = | |
Certificate::from_pem(&std::fs::read("./smtp.crt").unwrap()) | |
.unwrap(); | |
println!("cert: {:?}", cert); | |
let tls_parameters = TlsParameters::builder("smtp.example.com".into()) | |
.add_root_certificate(cert) | |
// .dangerous_accept_invalid_certs(true) | |
.build() | |
.unwrap(); | |
// SMTP credentials | |
let creds = Credentials::new( | |
"[email protected]".to_string(), | |
"your-password".to_string(), | |
); | |
// Create the SMTP transport with STARTTLS | |
let mailer = SmtpTransport::builder_dangerous("smtp.example.com") | |
.port(1025) | |
.tls(Tls::Required(tls_parameters)) | |
.credentials(creds) | |
.build(); | |
// Send the email | |
match mailer.send(&email) { | |
Ok(_) => println!("Email sent successfully!"), | |
Err(e) => println!("Could not send email: {:?}", e), | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment