Sending email in Rust: lettre and mailparse libraries for working with mail

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Sending email in Rust: lettre and mailparse libraries



Rust is a systems programming language that is increasingly used for building high-load web services and microservices. One of the typical tasks in such projects is sending emails. The Rust ecosystem has several mature libraries for working with email, among which lettre and mailparse stand out.



In this article, we will thoroughly examine how to configure sending emails in Rust using SMTP, how to add attachments and format HTML emails, as well as how to parse incoming messages. The material is aimed at developers already familiar with the basics of Rust.



The lettre library: the foundation for sending emails



Lettre is a modern, asynchronous and synchronous library for working with email in Rust. It supports:



  • SMTP transport (including STARTTLS and SSL/TLS);
  • Formatting emails in MIME format (text, HTML, attachments);
  • Working with encodings (Base64, Quoted-Printable);
  • Asynchronous transports via tokio or async-std.


To add lettre to your project, run the command:



cargo add lettrecargo add tokio --features full  # for asynchronous mode


Basic email sending via SMTP



Let's look at the simplest example — sending a text email via a Gmail SMTP server (or any other provider).



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

fn main() -> Result<(), Box<dyn std::error::Error>> { // Format the email let email = Message::builder() .from("Sender <from@example.com>".parse()?) .to("Recipient <to@example.com>".parse()?) .subject("Hello from Rust!") .body("This is a test email sent from Rust.".to_string())?;

// SMTP settings (example for Gmail) let creds = Credentials::new( "your_email@gmail.com".to_string(), "your_app_password".to_string(), );

// Create transport let mailer = SmtpTransport::relay("smtp.gmail.com")? .credentials(creds) .build();

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

Ok(())}


Note: For Gmail, you will need an app password, not your main password. For other providers (Yandex, Mail.ru, your own SMTP), change the server address and port.



Sending HTML emails with attachments



Lettre makes it easy to format multipart MIME messages. Let's add an HTML body and an attachment:



use lettre::message::header::ContentType;use lettre::message::{Attachment, MultiPart, SinglePart};use lettre::{Message, SmtpTransport, Transport};

fn send_html_with_attachment() -> Result<(), Box<dyn std::error::Error>> { // HTML body of the email let html_body = "<h1>Hello!</h1><p>This is an HTML email from Rust.</p>".to_string();

// Format the multipart email let email = Message::builder() .from("Sender <from@example.com>".parse()?) .to("Recipient <to@example.com>".parse()?) .subject("HTML email with attachment") .multipart( MultiPart::mixed() .singlepart( SinglePart::builder() .header(ContentType::TEXT_HTML) .body(html_body), ) .singlepart( Attachment::new("document.pdf".to_string()) .body(vec![0x25, 0x50, 0x44, 0x46], ContentType::parse("application/pdf")?), ), )?;

// Sending (similar to the previous example)

Recommendations