20 个版本
使用旧的 Rust 2015
0.3.2 | 2021 年 8 月 26 日 |
---|---|
0.3.0 | 2015 年 10 月 12 日 |
0.1.1 | 2015 年 7 月 22 日 |
0.0.9 | 2015 年 3 月 20 日 |
0.0.2 | 2014 年 11 月 21 日 |
#326 in 邮件
每月 52 次下载
74KB
1.5K SLoC
rust-smtp

警告:此库不再维护,已被 lettre
包取代。
安装
要使用此库,请将以下内容添加到您的 Cargo.toml
[dependencies]
smtp = "0.3"
许可证
本程序根据 MIT 许可证分发。
有关详细信息,请参阅 LICENSE。
lib.rs
:
Rust SMTP 客户端
此客户端应尽可能遵循 RFC 5321,但仍处于开发中。它旨在高效地将邮件从应用程序发送到中继邮件服务器,因为它尽可能依赖中继服务器进行合理性检查和 RFC 合规性检查。
它实现了以下扩展
它最终将实现以下扩展
- SMTPUTF8 (RFC 6531)
架构
此客户端分为三个主要部分
- client:提供所有 SMTP 命令的低级 SMTP 客户端
- sender:提供一种简单方法发送邮件的高级 SMTP 客户端
- email:与 sender 一起生成要发送的邮件
用法
简单示例
这是使用示例中最基本的示例
use smtp::sender::{Sender, SenderBuilder};
use smtp::email::EmailBuilder;
// Create an email
let email = EmailBuilder::new()
// Addresses can be specified by the couple (email, alias)
.to(("[email protected]", "Firstname Lastname"))
// ... or by an address only
.from("[email protected]")
.subject("Hi, Hello world")
.body("Hello world.")
.build();
// Open a local connection on port 25
let mut sender = SenderBuilder::localhost().unwrap().build();
// Send the email
let result = sender.send(email);
assert!(result.is_ok());
完整示例
use smtp::sender::{Sender, SenderBuilder};
use smtp::email::EmailBuilder;
use smtp::authentication::Mecanism;
use smtp::SUBMISSION_PORT;
let mut builder = EmailBuilder::new();
builder = builder.to(("[email protected]", "Alias name"));
builder = builder.cc(("[email protected]", "Alias name"));
builder = builder.from("[email protected]");
builder = builder.from("[email protected]");
builder = builder.sender("[email protected]");
builder = builder.subject("Hello world");
builder = builder.body("Hi, Hello world.");
builder = builder.reply_to("[email protected]");
builder = builder.add_header(("X-Custom-Header", "my header"));
let email = builder.build();
// Connect to a remote server on a custom port
let mut sender = SenderBuilder::new(("server.tld", SUBMISSION_PORT)).unwrap()
// Set the name sent during EHLO/HELO, default is `localhost`
.hello_name("my.hostname.tld")
// Add credentials for authentication
.credentials("username", "password")
// Use TLS with STARTTLS, you can also specify a specific SSL context
// with `.ssl_context(context)`
.starttls()
// Configure accepted authetication mecanisms
.authentication_mecanisms(vec![Mecanism::CramMd5])
// Enable connection reuse
.enable_connection_reuse(true).build();
let result_1 = sender.send(email.clone());
assert!(result_1.is_ok());
// The second email will use the same connection
let result_2 = sender.send(email);
assert!(result_2.is_ok());
// Explicitely close the SMTP transaction as we enabled connection reuse
sender.close();
直接使用客户端
如果您只想发送邮件而不使用 Email
提供标题
use smtp::sender::{Sender, SenderBuilder};
use smtp::email::SimpleSendableEmail;
// Create a minimal email
let email = SimpleSendableEmail::new(
"[email protected]",
"[email protected]",
"Hello world !"
);
let mut sender = SenderBuilder::localhost().unwrap().build();
let result = sender.send(email);
assert!(result.is_ok());
低级
您也可以发送命令,这里是一个简单的无错误处理的电子邮件事务
use smtp::client::Client;
use smtp::SMTP_PORT;
use smtp::client::net::NetworkStream;
let mut email_client: Client<NetworkStream> = Client::new();
let _ = email_client.connect(&("localhost", SMTP_PORT));
let _ = email_client.ehlo("my_hostname");
let _ = email_client.mail("[email protected]", None);
let _ = email_client.rcpt("[email protected]");
let _ = email_client.data();
let _ = email_client.message("Test email");
let _ = email_client.quit();
依赖项
~11MB
~183K SLoC