Skip to content

Instantly share code, notes, and snippets.

@zwpaper
Last active September 1, 2024 02:57
Show Gist options
  • Select an option

  • Save zwpaper/bca2917433c56c4b541397f4a6ac6516 to your computer and use it in GitHub Desktop.

Select an option

Save zwpaper/bca2917433c56c4b541397f4a6ac6516 to your computer and use it in GitHub Desktop.

Revisions

  1. zwpaper revised this gist Sep 1, 2024. 1 changed file with 5 additions and 4 deletions.
    9 changes: 5 additions & 4 deletions smtp-tls-client.rs
    Original file line number Diff line number Diff line change
    @@ -20,12 +20,13 @@ fn main() {
    .unwrap();

    let cert =
    Certificate::from_pem(&std::fs::read("smtp.crt").unwrap())
    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)
    // .dangerous_accept_invalid_certs(true)
    .build()
    .unwrap();

    @@ -37,7 +38,7 @@ fn main() {

    // Create the SMTP transport with STARTTLS
    let mailer = SmtpTransport::builder_dangerous("smtp.example.com")
    .port(587)
    .port(1025)
    .tls(Tls::Required(tls_parameters))
    .credentials(creds)
    .build();
    @@ -47,4 +48,4 @@ fn main() {
    Ok(_) => println!("Email sent successfully!"),
    Err(e) => println!("Could not send email: {:?}", e),
    }
    }
    }
  2. zwpaper revised this gist Aug 31, 2024. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions smtp-tls-client.rs
    Original file line number Diff line number Diff line change
    @@ -25,6 +25,7 @@ fn main() {

    let tls_parameters = TlsParameters::builder("smtp.example.com".into())
    .add_root_certificate(cert)
    // .dangerous_accept_invalid_certs(true)
    .build()
    .unwrap();

  3. zwpaper created this gist Aug 31, 2024.
    49 changes: 49 additions & 0 deletions smtp-tls-client.rs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,49 @@
    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();

    let tls_parameters = TlsParameters::builder("smtp.example.com".into())
    .add_root_certificate(cert)
    .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(587)
    .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),
    }
    }