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

释放双眼,带上耳机,听听看~!

功能介绍

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

准备工作

环境说明:

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

其它依赖安装准备:


1
2
3
4
5
6
1 sudo apt-get install openssl
2 sudo apt-get install libssl-dev
3 sudo apt install pkg-config
4 sudo apt install pkgconf
5
6

演示示例

  • 编写Cargo.toml,添加如下:


1
2
3
4
5
1[dependencies]
2lettre = "0.9"
3lettre_email = "0.9
4
5
  • 编写src/main.rs源码如下:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
1use lettre::smtp::authentication::Credentials;
2use lettre::{SmtpClient, Transport};
3use lettre_email::{EmailBuilder, Mailbox};
4
5fn main() {
6    let email = EmailBuilder::new()
7        .from(Mailbox::new("发送者的邮箱地址".to_string()))
8        //.from(Mailbox::new("xiaoming@163.com".to_string())) //发送者:xiaoming@163.com
9        .to(Mailbox::new("接收者邮箱地址".to_string()))
10        //.to(Mailbox::new("xiaohong@126.com".to_string())) //接收者:xiaohong@126.com
11        .subject("Test") //邮件标题
12        .body("This is a test email!") //邮件内容
13        .build()
14        .unwrap();
15
16    //for example: xiaoming@163.com, password: 123456
17    //let creds = Credentials::new("xiaoming".to_string(), "123456".to_string());
18    let creds = Credentials::new("你的邮箱用户名".to_string(), "你的邮箱密码".to_string());
19
20    //如163的邮箱就是smtp.163.com, 126的邮箱就是smtp.126.com
21    let mut mailer = SmtpClient::new_simple("邮箱服务器地址")
22        .unwrap()
23        .credentials(creds)
24        .transport();
25
26    let result = mailer.send(email.into());
27
28    if result.is_ok() {
29        println!("Email sent");
30    } else {
31        println!("Could not send email: {:?}", result);
32    }
33
34    assert!(result.is_ok());
35}
36
37

给TA打赏
共{{data.count}}人
人已打赏
安全技术

c++ list, vector, map, set 区别与用法比较

2022-1-11 12:36:11

安全运维

KMP 算法详解

2021-12-12 17:36:11

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索