Отправить на smtp.gmail.com:465 SSL с Lettre crate - PullRequest
0 голосов
/ 19 мая 2018

Я не могу отправить на SMTP-серверы по SSL.

Я использую lettre "0.8".

Я пытался использовать SmtpTransportBuilder из lettre :: smtp

use native_tls::TlsConnector;
use native_tls::{Protocol};
use lettre::smtp::authentication::{Credentials, Mechanism};
use lettre::{SimpleSendableEmail, EmailTransport, SmtpTransport, ClientTlsParameters, ClientSecurity};
use lettre::smtp::extension::ClientId;
use lettre::smtp::ConnectionReuseParameters;
use lettre::smtp::{SmtpTransportBuilder};

fn main() {
    env_logger::init();

    let email = SimpleSendableEmail::new(
        "from@host.com".to_string(),
        &["to@host.com".to_string()],
        "message_id".to_string(),
        "Hello world".to_string(),
    ).unwrap();

    pub const DEFAULT_TLS_PROT: &[Protocol] = &[Protocol::Sslv3];

    let mut tls_builder = TlsConnector::builder().unwrap();
    tls_builder.supported_protocols(DEFAULT_TLS_PROT).unwrap();

    let tls_parameters =
        ClientTlsParameters::new("smtp.gmail.com".to_string(), tls_builder.build().unwrap());

    pub const SUBMISSION_PORT: u16 = 465;

    let mut mailer = SmtpTransportBuilder::new(("smtp.gmail.com", SUBMISSION_PORT), ClientSecurity::Required(tls_parameters))
        .authentication_mechanism(Mechanism::Login)
        .credentials(Credentials::new("USER".to_string(), "PASS".to_string()))
        .connection_reuse(ConnectionReuseParameters::ReuseUnlimited)
        .build();


    let result_1 = mailer.send(&email);
    assert!(result_1.is_ok());

    mailer.close();
}

Замерзает на DEBUG 2018-05-19T18:45:09Z: lettre::smtp::client: connecting to 64.233.167.109:465, поэтому не продолжает AUTH.

1 Ответ

0 голосов
/ 19 мая 2018

Рабочий пример:

extern crate env_logger;
extern crate lettre;
extern crate lettre_email;
extern crate native_tls;

use lettre::smtp::authentication::{Credentials, Mechanism};
use lettre::smtp::ConnectionReuseParameters;
use lettre::smtp::SmtpTransportBuilder;
use lettre::{ClientSecurity, ClientTlsParameters, EmailTransport};
use lettre_email::EmailBuilder;
use native_tls::Protocol;
use native_tls::TlsConnector;

fn main() {
    env_logger::init();

    let email = EmailBuilder::new()
        .to(("to@example.com"))
        .from("from@example.com")
        .subject("Example subject")
        .text("Example text")
        .build()
        .unwrap();

    pub const DEFAULT_TLS_PROT: &[Protocol] = &[Protocol::Tlsv10];

    let mut tls_builder = TlsConnector::builder().unwrap();
    tls_builder.supported_protocols(DEFAULT_TLS_PROT).unwrap();

    let tls_parameters =
        ClientTlsParameters::new("smtp.gmail.com".to_string(), tls_builder.build().unwrap());

    pub const SUBMISSION_PORT: u16 = 465;

    let mut mailer = SmtpTransportBuilder::new(
        ("smtp.gmail.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();
}

Используется

lettre = "0.8"
lettre_email = "0.8"
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...