# br-email
----
[](LICENSE-MIT)
[](https://crates.io/crates/br-email)
Rust email library supporting IMAP, POP3, and SMTP with TLS, incremental sync, connection reuse, and automatic retry.
## Features
- **IMAP** - Receive emails via IMAP/TLS (port 993), UID-based incremental sync
- **POP3** - Receive emails via POP3/TLS (port 995), UIDL-based incremental sync
- **SMTP** - Send emails via SMTP/STARTTLS (port 25/587), powered by `lettre`
- **Email Parsing** - MIME parsing, headers, attachments, encoded content
- **Connection Reuse** - Cached connections with automatic reconnect on failure
- **Retry** - Exponential backoff (3 retries, 2s/4s/8s delays)
- **Thread Pool** - Parallel operations via `pools` module
## Usage
### Receive (IMAP)
```rust
use br_email::{Mail, Receive};
let mut mail = Mail::new("imap", "imap.gmail.com", 993, "name", "user@gmail.com", "app_password");
let mut recv = mail.receive();
let total = recv.get_total()?;
let email_data = recv.get_mail(1)?;
```
### Receive (POP3)
```rust
use br_email::{Mail, Receive};
let mut mail = Mail::new("pop3", "pop.gmail.com", 995, "name", "user@gmail.com", "app_password");
let mut recv = mail.receive();
let total = recv.get_total()?;
let email_data = recv.get_mail(1)?;
```
### Send (SMTP)
```rust
use br_email::{Mail, Sender};
let mut mail = Mail::new("smtp", "smtp.gmail.com", 587, "name", "user@gmail.com", "app_password");
let mut sender = mail.sender();
sender.set_from("user@gmail.com");
sender.set_to("recipient@example.com");
sender.set_subject("Hello");
sender.set_body("<p>Hello World</p>");
sender.send()?;
```
### Incremental Sync (IMAP - UID)
```rust
let mut recv = mail.receive();
let validity = recv.get_validity()?; // UIDVALIDITY - resync if changed
let max_uid = recv.get_max_uid()?; // Highest UID
let new_uids = recv.get_new_since("100")?; // UIDs > 100
```
### Incremental Sync (POP3 - UIDL)
```rust
let mut recv = mail.receive();
let uidl_map = recv.get_uidl_map()?; // Vec<(seq, uidl)>
// Compare with stored UIDLs to find new messages
```
### Parse Email
```rust
use br_email::Mail;
let data: Vec<u8> = /* raw email bytes */;
let parsed = Mail::analyze(data, false)?;
// Access: parsed.subject, parsed.from, parsed.body, parsed.attachments, etc.
```
### Config File
```rust
use br_email::{Config, Mail};
let config = Config::create("./br-email.toml")?;
let mut mail = Mail::new_new(config)?;
```
## Feature Flags
```toml
[dependencies]
br-email = "1.2" # All features (imap + pop3 + smtp)
br-email = { version = "1.2", default-features = false, features = ["imap"] } # IMAP only
```
| `imap` | Yes | IMAP receive support |
| `pop3` | Yes | POP3 receive support |
| `smtp` | Yes | SMTP send support (depends on `lettre`) |
## License
MIT