設定 Dovecot:在 Ubuntu 上建立 IMAP 和 POP3 伺服器

Dovecot IMAP POP3 Ubuntu
披露:Email Wiki 上的部分連結為 VPS 服務商(如 Vultr)的聯盟連結。透過這些連結購買,我們可能獲得佣金,但不會對您產生額外費用。這有助於我們維持網站免費。所有教程均獨立撰寫,我們僅推薦適合自建郵件伺服器的可靠服務。

Dovecot 是領先的開源 IMAP 和 POP3 伺服器。Postfix 負責收發郵件,而 Dovecot 則讓你的郵件用戶端(Thunderbird、Apple Mail 等)能夠讀取郵件。兩者組合構成自建郵件伺服器的完整架構。

前提條件

  • Ubuntu 22.04,已安裝 Postfix
  • 有效的網域 DNS 記錄
  • TLS 憑證(推薦 Let’s Encrypt)

第一步:安裝 Dovecot

sudo apt update
sudo apt install dovecot-core dovecot-imapd dovecot-pop3d -y
dovecot --version

第二步:設定 Maildir 儲存

編輯 /etc/dovecot/conf.d/10-mail.conf

mail_location = maildir:~/Maildir

第三步:設定認證

編輯 /etc/dovecot/conf.d/10-auth.conf

disable_plaintext_auth = yes
auth_mechanisms = plain login

第四步:啟用 TLS

編輯 /etc/dovecot/conf.d/10-ssl.conf

ssl = required
ssl_cert = </etc/letsencrypt/live/mail.example.com/fullchain.pem
ssl_key  = </etc/letsencrypt/live/mail.example.com/privkey.pem
ssl_min_protocol = TLSv1.2
ssl_cipher_list = HIGH:!SSLv3:!aNULL

第五步:設定監聽埠

編輯 /etc/dovecot/conf.d/10-master.conf

service imap-login {
  inet_listener imap { port = 143 }
  inet_listener imaps { port = 993; ssl = yes }
}
service pop3-login {
  inet_listener pop3 { port = 110 }
  inet_listener pop3s { port = 995; ssl = yes }
}

第六步:與 Postfix 整合(SASL)

10-master.conf 中新增認證套接字:

service auth {
  unix_listener /var/spool/postfix/private/auth {
    mode = 0660
    user = postfix
    group = postfix
  }
}

在 Postfix 的 /etc/postfix/main.cf 中新增:

smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth
smtpd_sasl_auth_enable = yes

重新載入:

sudo systemctl reload dovecot postfix

第七步:測試 IMAP 連線

openssl s_client -connect mail.example.com:993

連線成功後會看到:

* OK [CAPABILITY IMAP4rev1 ...] Dovecot ready.

常見問題

「認證失敗」 — 確認用戶端使用了 SSL 連線,查看 /var/log/mail.log/var/log/auth.log

Maildir 目錄權限問題 — 手動建立目錄:sudo -u testuser mkdir -p ~/Maildir/{cur,new,tmp}

下一步

Postfix + Dovecot 正常運行後,下一步是設定郵件認證(SPF、DKIM、DMARC)

準備好部署你的郵件伺服器了嗎?

你需要一台 IP 信譽乾淨的可靠 VPS。立即使用 Vultr——獨立 IP、NVMe SSD、全球資料中心,最低 $6/月。