发送到smtp.gmail.com:465带有Lettre箱子的SSL

时间:2018-05-19 18:47:53

标签: rust

我无法发送到SSL上的smtp服务器。

我正在使用lettre“0.8”。

我尝试过使用lettre :: smtp

中的SmtpTransportBuilder
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 :(得分:5)

一个工作示例:

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"