014 Rust 网络编程,邮件发送的示例

功能介绍

本实例主要是使用lettre和letter-email实现在程序中发送邮件的功能。

准备工作

环境说明:

  • 操作系统:ubuntu18.04
  • Rust版本:1.41.0

其它依赖安装准备:

 sudo apt-get install openssl
 sudo apt-get install libssl-dev
 sudo apt install pkg-config
 sudo apt install pkgconf

演示示例

  • 编写Cargo.toml,添加如下:
    [dependencies]
    lettre = "0.9"
    lettre_email = "0.9
  • 编写src/main.rs源码如下:
    use lettre::smtp::authentication::Credentials;
    use lettre::{SmtpClient, Transport};
    use lettre_email::{EmailBuilder, Mailbox};
    

fn main() {
let email = EmailBuilder::new()
.from(Mailbox::new(“发送者的邮箱地址”.to_string()))
//.from(Mailbox::new(“xiaoming@163.com“.to_string())) //发送者:xiaoming@163.com
.to(Mailbox::new(“接收者邮箱地址”.to_string()))
//.to(Mailbox::new(“xiaohong@126.com“.to_string())) //接收者:xiaohong@126.com
.subject(“Test”) //邮件标题
.body(“This is a test email!”) //邮件内容
.build()
.unwrap();

//for example: xiaoming@163.com, password: 123456
//let creds = Credentials::new("xiaoming".to_string(), "123456".to_string());
let creds = Credentials::new("你的邮箱用户名".to_string(), "你的邮箱密码".to_string());

//如163的邮箱就是smtp.163.com, 126的邮箱就是smtp.126.com
let mut mailer = SmtpClient::new_simple("邮箱服务器地址") 
    .unwrap()
    .credentials(creds)
    .transport();

let result = mailer.send(email.into());

if result.is_ok() {
    println!("Email sent");
} else {
    println!("Could not send email: {:?}", result);
}

assert!(result.is_ok());

}

本作品采用《CC 协议》,转载必须注明作者和本文链接
令狐一冲
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!
文章
255
粉丝
119
喜欢
308
收藏
128
排名:335
访问:2.8 万
私信
所有博文
社区赞助商