Rust 邮件功能库 lettre 介绍

库介绍

lettre是一个Rust邮件相关的库,其名字为法语的信笺。

github地址

https://github.com/lettre/lettre

网站

https://lettre.at

特性简单说明

  • 多种传输方式
  • 支持Unicode
  • 使用了SMTP安全传输
  • 易于使用

    实战

    创建工程

    cargo new hello-lettre

    配置依赖

    在Cargo.toml文件的[dependencies]标签下添加lettre的引用:

    [dependencies]
    lettre = "0.9"
    lettre_email = "0.9"
    mime = "0.3.13"

    示例程序

    extern crate lettre;
    extern crate lettre_email;
    extern crate mime;
    use lettre_email::Email;
    use lettre::smtp::authentication::Credentials;
    use lettre::{SmtpClient, Transport};
    fn main() {
    let email_receiver = "YOUR_TARGET_EMAIL";
    let mine_email = "YOUR_GMAIL_ADDRESS";
    let smtp_server = "smtp.gmail.com";
    let password = "YOUR_GMAIL_APPLICATION_PASSWORD"; //需要生成应用专用密码
    
    let email = Email::builder()
        .to(email_receiver)
        .from(mine_email)
        .subject("subject")
        .html("<h1>Hi there</h1>")
        .text("Message send by lettre Rust")
        .build()
        .unwrap();
    
    let creds = Credentials::new(
        mine_email.to_string(),
        password.to_string(),
    );
    
    // Open connection to Gmail
    let mut mailer = SmtpClient::new_simple(smtp_server)
        .unwrap()
        .credentials(creds)
        .transport();
    
    // Send the email
    let result = mailer.send(email.into());
    
    if result.is_ok() {
        println!("Email sent");
    } else {
        println!("Could not send email: {:?}", result);
    }
    
    print!("{:?}", result);
    mailer.close();
    }

令狐一冲

讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!