1 # SMTP and IMAP Mail Server
2
3 A basic SMTP and IMAP server implementation in Rust with TLS/STARTTLS support.
4
5 ## Features
6
7 ### SMTP
8 - ✅ Basic SMTP protocol support (HELO/EHLO, MAIL FROM, RCPT TO, DATA, QUIT)
9 - ✅ STARTTLS support for secure connections
10 - ✅ Self-signed certificate generation for TLS
11 - ✅ Multiple recipient support
12 - ✅ Command validation and error handling
13 - ✅ Dual-mode operation: separate ports for incoming and outgoing mail
14
15 ### IMAP
16 - ✅ IMAP4rev1 protocol implementation
17 - ✅ Essential commands (LOGIN, SELECT, EXAMINE, FETCH, SEARCH, LIST, STATUS, LOGOUT, CAPABILITY)
18 - ✅ Mailbox management (INBOX, Sent, Drafts, Trash)
19 - ✅ Message storage and retrieval
20 - ✅ UID and sequence number support
21 - ✅ Message flags (Seen, Answered, Flagged, Deleted, Draft, Recent)
22 - ✅ Protocol state management
23
24 ### General
25 - ✅ Async/await with Tokio runtime
26 - ✅ Structured logging with tracing
27 - ✅ Shared message storage between SMTP and IMAP
28
29 ## Running the Server
30
31 ```bash
32 cargo run
33 ```
34
35 The SMTP server listens on two ports:
36 - `127.0.0.1:2525` - **Receive mode**: For incoming mail from other servers (messages stored in INBOX)
37 - `127.0.0.1:2526` - **Submit mode**: For outgoing mail submission from clients (messages logged and relayed)
38
39 The IMAP server listens on `127.0.0.1:1143` by default.
40
41 ## Testing
42
43 ### Testing SMTP
44
45 You can test the SMTP server using `nc` (netcat) or `telnet`:
46
47 **Testing receive mode (port 2525):**
48 ```bash
49 echo -e "EHLO test.com\r\nMAIL FROM: <sender@example.com>\r\nRCPT TO: <recipient@example.com>\r\nDATA\r\nSubject: Test\r\n\r\nTest message\r\n.\r\nQUIT\r\n" | nc localhost 2525
50 ```
51
52 **Testing submit mode (port 2526):**
53 ```bash
54 echo -e "EHLO test.com\r\nMAIL FROM: <sender@example.com>\r\nRCPT TO: <recipient@example.com>\r\nDATA\r\nSubject: Outgoing Test\r\n\r\nOutgoing message\r\n.\r\nQUIT\r\n" | nc localhost 2526
55 ```
56
57 ### Testing IMAP
58
59 You can test the IMAP server using `nc` (netcat) or `telnet`:
60
61 ```bash
62 # Connect and login
63 echo -e "a1 CAPABILITY\r\na2 LOGIN user pass\r\na3 LIST \"\" \"*\"\r\na4 SELECT INBOX\r\na5 LOGOUT\r\n" | nc localhost 1143
64
65 # Fetch messages
66 echo -e "a1 LOGIN user pass\r\na2 SELECT INBOX\r\na3 FETCH 1 (FLAGS BODY[])\r\na4 LOGOUT\r\n" | nc localhost 1143
67 ```
68
69 ## Supported Commands
70
71 ### SMTP Commands
72
73 - `HELO/EHLO` - Initiate SMTP session
74 - `MAIL FROM` - Specify sender email address
75 - `RCPT TO` - Specify recipient email address (supports multiple)
76 - `DATA` - Begin email message content
77 - `RSET` - Reset the current transaction
78 - `NOOP` - No operation (keepalive)
79 - `QUIT` - Close connection
80 - `STARTTLS` - Upgrade connection to TLS
81
82 ### IMAP Commands
83
84 - `CAPABILITY` - List server capabilities
85 - `LOGIN` - Authenticate user
86 - `LIST/LSUB` - List available mailboxes
87 - `SELECT` - Select a mailbox (read-write)
88 - `EXAMINE` - Select a mailbox (read-only)
89 - `FETCH` - Retrieve message data
90 - `SEARCH` - Search for messages
91 - `STATUS` - Get mailbox status
92 - `LOGOUT` - Close connection
93 - `NOOP` - No operation (keepalive)
94
95 ## Building
96
97 ```bash
98 cargo build --release
99 ```
100
101 ## Dependencies
102
103 - `tokio` - Async runtime
104 - `anyhow` - Error handling
105 - `tracing` / `tracing-subscriber` - Logging
106 - `regex` - SMTP command parsing
107 - `native-tls` / `tokio-native-tls` - TLS support
108 - `openssl` - Certificate generation
109 - `futures` - Stream handling
110 - `tokio-util` - Codec utilities