Is it possible to use "lettre" crate to send email from a computer behind a proxy?


I need to send an email from a rust program. while I was googling to see available solutions I found Lettre crate. I took the sample program provided in that page :

use lettre::transport::smtp::authentication::Credentials;
use lettre::{Message, SmtpTransport, Transport};

fn main()
    let email = Message::builder()
        .from("Here I wrote the email address of the sender".parse().unwrap())
        .to("Here I wrote the email address of the destination".parse().unwrap())
        .subject("Happy new year")
        .body(String::from("Be happy!"))

    let creds = Credentials::new(
        "Once again, here I wrote the email address of the sender".to_string(),
        "And here I wrote the password of the sender's email account".to_string()

    // Open a remote connection to gmail
    let mailer = SmtpTransport::relay("")

    // Send the email
    match mailer.send(&email) {
        Ok(_) => println!("Email sent successfully!"),
        Err(e) => panic!("Could not send email: {:?}", e),

The program compiled properly by downloading all dependencies specified in Cargo.toml. However when I run the program it panicked with the following error message :

thread 'main' panicked at 'Could not send email: lettre::transport::smtp::Error { kind: Connection, source: Os { code: 101, kind: NetworkUnreachable, message: "Network is unreachable" } }', src/
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

The computer on which I run the program is inside a network protected by a proxy and I think that could explain the error message. In my Python programs for example, whenever I use Requests module to send HTTP queries to our RESTful APIs, indeed I need to specify the proxy in our network as a parameter of my query, otherwise the query will not pass. Therefore, I checked the documentation of lettre::SmtpTransport but I didn't find anything related to proxy.

So, I would like to know whether it is possible to use this crate to send email from a computer behind a proxy.

Thanks in advance

You can't make an SMTP connection through an HTTP proxy. Also, being on a network where HTTP is proxied implies to me that the responsible network security people would take a dim view of random machines making arbitrary SMTP connections to the outside. Perhaps there is a dedicated SMTP relay somewhere on the network?

1 Like

Actually, the purpose is to send an email to three employees of the same organization. It's an application that analyzes an API and upon any detected anomaly produces a report that should be sent as an alert via email to these people.

You can make any sort of TCP connection via HTTP CONNECT or Socks (if the proxy allows it), but I'm afraid I don't know of a crate that hands you back the underlying TCP tunnel offhand... or a way to use an existing connection in lettre for that matter, so probably this would be a feature request to them.

Is email sent from within the organization (on premise)? You might just need the internal smtp server address.

1 Like

Yes all receivers work in the same building and I would say probably even the same subnet/VLAN (I should verify to confirm the latter). Isn't any other crate with support for proxy?

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.