Lettre specifying smtp server port

Can smtp servert port be specified in lettre?

[dependencies]
lettre = "0.10.0-rc.3"
use lettre::transport::smtp::authentication::Credentials;
use lettre::{Message, SmtpTransport, Transport};
fn main() {
    let smtp_port = 587u16;

    let email = Message::builder()
        .from("From <exampleaccount@gmail>".parse().unwrap())
        .reply_to("Re-to <exampleaccount@gmail>".parse().unwrap())
        .to("To address <hello@example.com>".parse().unwrap())
        .subject("Hello world")
        .body(String::from("hello world!"))
        .unwrap();

    let creds = Credentials::new("username".to_string(), "password".to_string());

    // Open a remote connection to gmail
    let mailer = SmtpTransport::relay("smtp.googlemail.com")
        .unwrap()
        .credentials(creds)
        .build();

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

Does it accept:

SmtpTransport::relay("smtp.googlemail.com:587")
1 Like

hmm, It kinda worked better, got a different error: error from the smtp server "host not known".

Looking at the docs it reads:

Creates an encrypted transport over submissions port

So the relay parameter is really just the host.
But you get back a SmtpTransportBuilder, which has a port(self, port: u16) method, so just use that.

2 Likes

Im having trouble implementing SmtpTransportBuilder.

According to the docs some things does not seem to exist in current api?
See ClientSecurity in lettre::smtp - Rust

[dependencies]
lettre = "0.10.0-rc.3"
use lettre::smtp::ClientSecurity;
use lettre::transport::smtp::authentication::Credentials;
use lettre::transport::smtp::SmtpTransportBuilder;
use lettre::{ClientTlsParameters, EmailTransport, Message};

use lettre::{SmtpTransport, Transport};

fn main() {
    let email = Message::builder()
        .from("From <exampleaccount@gmail>".parse().unwrap())
        .reply_to("Re-to <exampleaccount@gmail>".parse().unwrap())
        .to("To address <hello@example.com>".parse().unwrap())
        .subject("Hello world")
        .body(String::from("hello world!"))
        .unwrap();

    let creds = Credentials::new("username".to_string(), "password".to_string());

    pub const SUBMISSION_PORT: u16 = 465;

    let mut mailer = SmtpTransportBuilder::new(
        ("smtp.googlemail.com", SUBMISSION_PORT),
        ClientSecurity::Wrapper(tls_parameters),
    )
    .expect("Failed to create transport")
    .authentication_mechanism(Mechanism::Login)
    .credentials(Credentials::new(
        "example".to_string(),
        "example".to_string(),
    ))
    .connection_reuse(ConnectionReuseParameters::ReuseUnlimited)
    .build();

    println!("{:?}", mailer.send(&email));

    mailer.close();
}

Hello @Gorilla1

The code snippet below works for me

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

fn main() {
    let email = Message::builder()
        .from("NoBody <from@email.ch>".parse().unwrap())
        .reply_to("Yuin <from@email.ch>".parse().unwrap())
        .to("Hei <to@email.ch>".parse().unwrap())
        .subject("Happy new year")
        .body(String::from("Be happy!"))
        .unwrap();

    let creds = Credentials::new("your_username".to_string(), "very_secret_password".to_string());

    // Open a remote connection to gmail
    let mailer = SmtpTransport::starttls_relay("my_start_tls_relay.ch")
        .unwrap()
        .credentials(creds)
        .build();

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

Watch out - I'm having an email server configured using port 587 and STARTTLS. Using the same lettre version as you mentioned - compiled with Rust 1.53.0.
Note: it is just the example mentioned on github https://github.com/lettre/lettre - with some small adaptions.

Stefan

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.