如何在Ubuntu 16.04 LTS本地搭建简易邮件服务器?
Hey Alfred, I’ve got you covered with a dead-simple local test mail server setup for Ubuntu 16.04 LTS that checks all your boxes. We’ll use Postfix (for sending mail) and Dovecot (for retrieving mail) – they’re lightweight, easy to tweak, and work entirely offline. Let’s dive in:
First, update your package list and install the core packages:
sudo apt update && sudo apt install postfix dovecot-core dovecot-imapd
When the Postfix setup wizard pops up:
- Select Local only (this ensures the server only listens to your local machine, no internet needed)
- Set the system mail name to
localhost(this will be our domain for the email addresses)
Your sender@localhost and receiver@localhost addresses map directly to local Linux users. Let’s create them and set passwords (you’ll need these for Thunderbird later):
# Create sender user and set password sudo useradd -m -s /bin/bash sender sudo passwd sender # Create receiver user and set password sudo useradd -m -s /bin/bash receiver sudo passwd receiver
The -m flag creates a home directory for each user (where their mail will be stored), and -s sets a default shell.
Edit Postfix’s main configuration file to lock it down for local use:
sudo nano /etc/postfix/main.cf
Make sure these settings are present (adjust if they’re missing or incorrect):
myhostname = localhost mydomain = localhost myorigin = $mydomain inet_interfaces = loopback-only # Only listen to local connections mydestination = $myhostname, localhost.$mydomain, localhost home_mailbox = Maildir/ # Use Maildir format (Dovecot-friendly)
Save the file (Ctrl+O, Enter, then Ctrl+X) and restart Postfix to apply changes:
sudo systemctl restart postfix
Dovecot handles retrieving mail via IMAP (which Thunderbird uses). Let’s tweak its settings:
1. Enable IMAP
Edit the main Dovecot config:
sudo nano /etc/dovecot/dovecot.conf
Find the protocols line and set it to:
protocols = imap
2. Set Mail Storage Location
Edit the mail storage config:
sudo nano /etc/dovecot/conf.d/10-mail.conf
Set the mail_location to:
mail_location = maildir:~/Maildir
3. Allow Plaintext Authentication (Local Only)
Since we’re only using this server locally, plaintext auth is safe. Edit the auth config:
sudo nano /etc/dovecot/conf.d/10-auth.conf
Update these lines:
disable_plaintext_auth = no auth_mechanisms = plain login
4. Restart Dovecot
Apply all changes by restarting Dovecot:
sudo systemctl restart dovecot
Let’s verify sending mail works before setting up Thunderbird:
- Switch to the sender user:
su - sender - Send a test email to the receiver:
echo "This is a test email body!" | mail -s "Local Test Email" receiver@localhost - Switch to the receiver user and check for the mail:
su - receiver ls ~/Maildir/new/ # You should see a new file here cat ~/Maildir/new/* # View the email content
If you see the email content, the server is working locally!
Now let’s set up Thunderbird to use your local server:
For sender@localhost:
- Open Thunderbird → Click Email under "Create a new account"
- Enter:
- Your name: (e.g., "Test Sender")
- Email address:
sender@localhost - Password: (the password you set for the
senderuser)
- Click Continue – Thunderbird might fail auto-config, so select Manual config
- Set these values:
- Incoming (IMAP)
- Server hostname:
localhost - Port:
143 - Connection security:
None - Authentication method:
Normal Password - Username:
sender
- Server hostname:
- Outgoing (SMTP)
- Server hostname:
localhost - Port:
25 - Connection security:
None - Authentication method:
Normal Password - Username:
sender
- Server hostname:
- Incoming (IMAP)
- Click Done to finish setup
Repeat the exact same steps for receiver@localhost (use the receiver’s password and username).
- If mail isn’t sending/receiving, check the logs:
- Postfix logs:
sudo tail -f /var/log/mail.log - Dovecot logs:
sudo tail -f /var/log/dovecot.log
- Postfix logs:
- Ensure your firewall allows local connections to ports 143 (IMAP) and 25 (SMTP) – Ubuntu 16.04’s default firewall usually allows this, but you can verify with
sudo ufw status
内容的提问来源于stack exchange,提问作者Alfred

