邮件发送这个在Rust中总是有点问题,之前用lettre低版本测试没问题,后来腾讯企业邮箱升级做了一些限制调整,我调了好长时间,终于调通了。
Cargo.toml
[dependencies]
lettre = "0.10"
目前lettre最新版本就是0.10版本了。
sample.rs
use lettre::message::header::ContentType;
use lettre::transport::smtp::authentication::{Credentials, Mechanism};
use lettre::transport::smtp::client::{TlsParameters, Tls};
use lettre::{Message, SmtpTransport, Transport};
use log::{info, warn};
use crate::router;
fn send_mail(to_addr: &str, subject: &str, body: String) -> anyhow::Result<()> {
let email: Message = Message::builder()
.from("Qttc <blog@qttc.net>".parse()?)
.reply_to("Qttc <blog@qttc.net>".parse()?)
.to(to_addr.parse()?)
.subject(subject)
.header(ContentType::TEXT_HTML)
.body(body)?;
let creds: Credentials = Credentials::new(
"[Account]".to_string(),
"[Password]".to_string(),
);
let smtp_host: &str = "smtp.exmail.qq.com";
let tls_params: TlsParameters = TlsParameters::new("exmail.qq.com".to_string())?;
let tls: Tls = Tls::Wrapper(tls_params);
let mailer: SmtpTransport = SmtpTransport::relay(smtp_host)?
.port(465)
.credentials(creds)
.authentication(vec![Mechanism::Login])
.tls(tls)
.build();
match mailer.send(&email) {
Ok(_) => info!("Email sent {} successfully!", &to_addr),
Err(e) => warn!("Error sending email {} was error: {}", &to_addr, e),
};
Ok(())
}
...