Email Protocols
SMTP, POP3, and IMAP protocols
Email Protocols
Email relies on three main protocols for sending and receiving messages.
SMTP (Simple Mail Transfer Protocol)
Handles outgoing email
Basics
- Port 25 (unencrypted)
- Port 587 (TLS)
- Port 465 (SSL/TLS)
- Push protocol (client to server)
- Used for sending email
How SMTP Works
1. Client connects to SMTP server
2. MAIL FROM: Sender address
3. RCPT TO: Recipient address
4. DATA: Message content
5. . (period): End of message
6. QUIT: Close connectionSMTP Commands
# Connect to SMTP server
telnet mail.example.com 25
# Commands
HELO mail.example.com
MAIL FROM:<sender@example.com>
RCPT TO:<recipient@example.com>
DATA
Subject: Test
This is a test email.
.
QUITPOP3 (Post Office Protocol v3)
Handles incoming email
Basics
- Port 110 (unencrypted)
- Port 995 (SSL/TLS)
- Pull protocol (server to client)
- Downloads email to client
- Typically deletes from server
How POP3 Works
1. Client connects to POP3 server
2. USER: Provide username
3. PASS: Provide password
4. STAT: Check message count
5. RETR: Retrieve message
6. DELE: Delete message
7. QUIT: Close connectionIMAP (Internet Message Access Protocol)
Advanced incoming email protocol
Basics
- Port 143 (unencrypted)
- Port 993 (SSL/TLS)
- Pull protocol (server to client)
- Keeps email on server
- Supports folders and flags
- Preferred for modern clients
Why IMAP?
- Access mail from multiple devices
- Folder support
- Flag/tag support
- Partial download
- Server-side search
POP3 vs IMAP
POP3 IMAP
Port 110/995 Port 143/993
Download only Sync
Local storage Server storage
Simple Complex
Basic features Advanced features
Good for mobile Better for desktopEmail Security
SMTP TLS/SSL
# Port 587 (STARTTLS)
# Port 465 (Implicit SSL)
# Configure in postfix
# /etc/postfix/main.cf
smtp_tls_security_level = encrypt
smtp_use_tls = yesSPF (Sender Policy Framework)
DNS record specifying authorized mail servers
v=spf1 include:_spf.example.com ~allDKIM (DomainKeys Identified Mail)
Digitally signs emails
DMARC (Domain-based Message Authentication)
Policy enforcement for SPF and DKIM
v=DMARC1; p=reject; rua=mailto:admin@example.comEmail Server Setup
Postfix (SMTP)
# Install
sudo apt install postfix
# Configuration
/etc/postfix/main.cf
# Start
sudo systemctl start postfixDovecot (POP3/IMAP)
# Install
sudo apt install dovecot-core dovecot-imapd dovecot-pop3d
# Configuration
/etc/dovecot/dovecot.conf
# Start
sudo systemctl start dovecotTesting Email
# Send test email via SMTP
sendmail user@example.com << EOF
Subject: Test
Test message
EOF
# Check mail queue
postqueue -p
# Flush queue
postqueue -f
# Check logs
sudo tail -f /var/log/mail.logCommon Email Ports
25 - SMTP (unencrypted)
110 - POP3 (unencrypted)
143 - IMAP (unencrypted)
465 - SMTPS (SSL)
587 - SMTP TLS
993 - IMAPS (SSL)
995 - POP3S (SSL)Best Practices
- Use TLS/SSL encryption
- Implement SPF, DKIM, DMARC
- Monitor email queue
- Keep email software updated
- Implement authentication
- Monitor for abuse
- Use strong passwords
- Backup email data
- Implement rate limiting
- Monitor for spam and phishing