跳到主要内容

Rust发送邮件,带SSL

引入依赖:

lettre = "0.10"

代码部分

默认端口为:465

extern crate lettre;

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

fn main() {

// 设置SMTP服务器信息
let smtp_server = "smtp.exmail.qq.com";
let smtp_username = "smtp_username";
let smtp_password = "password";

// 设置发件人和收件人
let from_address = "from_email_address";
let to_address = "to_email_address";

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

let email = Message::builder()
.from(from_address.parse().unwrap())
.to(to_address.parse().unwrap())
.subject("Sending email with Rust")
.body(String::from("This is my first email"))
.unwrap();

let creds = Credentials::new(smtp_username.to_string(), smtp_password.to_string());

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

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

}