Akemi

Postfix搭建内网环境邮件服务器

2025/04/08

电子邮件基本概念

类型 名称 协议 核心功能 常见示例
MUA 邮件用户代理(用户端) IMAP, POP3 用户收发邮件的客户端工具,负责编辑、发送邮件,以及从服务器拉取邮件到本地。 Outlook, Thunderbird, Foxmail
MSA 邮件提交代理
(发件) SMTP(端口 587) 接收 MUA 提交的邮件,检查格式/权限等,并将邮件转发给同服务器的 MTA。 Postfix, Sendmail(作为提交网关)
MTA 邮件传输代理(传输) SMTP(端口 25) 负责邮件的路由和转发,将邮件从一个服务器传递到另一个服务器(或 MDA)。 Postfix, Sendmail, Exim
MDA 邮件投递代理(存储) 无特定协议 将 MTA 接收的邮件最终投递到用户邮箱(磁盘存储),可能执行过滤(反垃圾、病毒扫描)。 Procmail, Maildrop
MRA 邮件接收代理(读取) IMAP, POP3 响应 MUA 的请求,通过 IMAP/POP3 协议从用户邮箱中读取邮件并返回给客户端。 Dovecot, Cyrus

相关概念

SMTP(Simple Mail Transfer Protocol)传输发送邮件所使用的标准协议,发往25端口;
IMAP(Internet Message Access Protocol)接收邮件使用的标准协议之一;
POP3(Post Office Protocol 3) 接收邮件使用的标准协议之一,使用110端口。

邮件服务器基本都有MTA,MDA,MRA 组成。
常用的MUA有:outlook、foxmail;
常用的MTA服务有:sendmail、postfix(升级版);
常用的MDA有:procmail、dropmail;
常用的MRA有:dovecot。

邮件发送与接收流程

同邮件服务器注册的两个账号的邮件传输:
1.用户客户端MUA发送邮件到邮件服务器25端口,DNS查询qq.com的MX记录,如mail.qq.com
2.mail.qq.com查找163.com的MX DNS记录,通过MDA将邮件relay到mail.163.com
3.目标客户端从mail.163.com将邮件下载下来

使用postfix搭建MTA

搭建dns服务器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

yum -y install unbound
vim /etc/unbound/unbound.conf
interface: 0.0.0.0
access-control: 0.0.0.0/0 allow

# 本地域配置
cat /etc/unbound/local.d/wangsheng.com.conf
local-zone: "wangsheng.com." static
local-data: "wangsheng.com. IN SOA ns.wangsheng.com. root.wangsheng.com. 1 1h 1h 1h 1h"
local-data: "wangsheng.com. IN NS ns.wangsheng.com."
local-data: "ns.wangsheng.com. IN A 10.163.2.100"

local-data: "wangsheng.com. IN MX 0 mail.wangsheng.com."
local-data: "mail.wangsheng.com. IN A 10.163.2.106"

# 转发域配置,忽略DNSSEC
cat /etc/unbound/conf.d/forward.com.conf
server:
domain-insecure: "com."
domain-insecure: "net."
domain-insecure: "org."
forward-zone:
name: "."
forward-addr: 114.114.114.114

unbound-checkconf
unbound-control-setup

systemctl enable unbound --now

host mail.wangsheng.com.
mail.wangsheng.com has address 10.163.1.106

postfix搭建SMTP邮件服务器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
yum -y install postfix

ls /etc/postfix/
access dynamicmaps.cf.d/ main.cf master.cf.proto relocated
canonical generic main.cf.proto postfix-files transport
dynamicmaps.cf header_checks master.cf postfix-files.d/ virtual
# 主要修改main.cf

# 查看 Postfix 所有默认配置参数
postconf -d

# 设置邮件服务器主机名(FQDN格式)
postconf -e "myhostname=mail.wangsheng.com"

# 定义邮件服务器的域名(主域名部分)
postconf -e "mydomain=wangsheng.com"

# 设置外发邮件的域名来源标识
postconf -e "myorigin=wangsheng.com"

# 允许在所有网络接口上监听邮件请求
postconf -e "inet_interfaces=all"

# 指定本机接收邮件的目标域名列表(逗号分隔)
postconf -e "mydestination=mail.wangsheng.com,localhost.wangsheng.com,localhost,wangsheng.com"

# 允许所有IP地址通过本服务器转发邮件(生产环境需限制)
postconf -e "mynetworks=0.0.0.0/0"

# 定义允许邮件中转的域名/主机列表
postconf -e "relay_domains=mail.wangsheng.com,localhost.wangsheng.com,localhost,wangsheng.com"

# 设置邮件存储格式为 Maildir
postconf -e "home_mailbox=Maildir/"

systemctl disable firewalld --now
setenforce 0
systemctl enable postfix --now

测试发送邮件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
yum -y install mailx # centos7
yum -y install s-nail # centos8及新版本
echo "this is a test message from local" | s-nail -s "Test Mail" root@wangsheng.com
echo "this is a test message from local" | mail -s "Test Mail" root@wangsheng.com

看一眼日志:
tail -f /var/log/maillog
Apr 7 18:20:28 mail-client postfix/smtp[16362]: 82B045C17: to=<root@wangsheng.com>, relay=mail.wangsheng.com[10.163.2.106]:25, delay=0.05, delays=0.02/0.01/0/0.02, dsn=2.0.0, status=sent (250 2.0.0 Ok: queued as 80FA74052B1)

# 使用mutt查看邮件
yum -y install mutt
mutt -f /root/Maildir
q:Quit d:Del u:Undel s:Save m:Mail r:Reply g:Group ?:Help
1 N Apr 07 root (0.1K) Test Mail
2 N Apr 07 root (0.1K) Test Mail
3 N Apr 07 root (0.1K) Test Mail
4 N Apr 07 root (0.1K) Test Mail
5 N Apr 07 root (0.1K) Test Mail from local
6 N Apr 07 root (0.1K) Test Mail from local
7 N Apr 07 root (0.1K) Test Mail from local
8 N Apr 07 root (0.1K) Test Mail from local
9 N Apr 07 root (0.1K) Test Mail from local
10 N Apr 07 root (0.1K) Test Mail from local
11 N Apr 07 root (0.1K) Test Mail from local
12 N Apr 07 root (0.1K) Test Mail from local
13 N Apr 07 root (0.1K) Test Mail from local
14 N Apr 07 root (0.1K) Test Mail from local
15 N Apr 07 root (0.1K) Test Mail from local
16 N Apr 07 root (0.1K) Test Mail from local
17 N Apr 07 root (0.1K) Test Mail from local
18 N Apr 07 root (0.1K) Test Mail from local
19 N Apr 07 root (0.1K) Test Mail from local
20 N Apr 07 root (0.1K) Test Mail from local
21 N Apr 07 root (0.1K) Test Mail from local
22 N Apr 07 root (0.1K) Test Mail from local
23 N Apr 07 root (0.1K) Test Mail from local
24 N Apr 07 root (0.1K) Test Mail from local

使用dovecot搭建MRA

使MUA可以通过MRA接受邮件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
yum -y install dovecot

# === Postfix 配置部分 ===
# 设置 Postfix 使用 Dovecot 作为 SASL 认证
postconf -e "smtpd_sasl_type=dovecot"
# 指定 SASL 认证的 socket 路径(Dovecot 的认证服务路径)
postconf -e "smtpd_sasl_path=private/auth"
# 启用 SMTP 身份验证(允许客户端登录)
postconf -e "smtpd_sasl_auth_enable=yes"
# 设置本地 SASL 认证的域名(邮件服务器域名)
postconf -e "smtpd_sasl_local_domain=mail.wangsheng.com"
# 禁止匿名登录(强制要求有效凭证)
postconf -e "smtpd_sasl_security_options=noanonymous"
# 设置收件人过滤规则:允许本地网络、已认证用户,拒绝未经认证的目标
postconf -e "smtpd_recipient_restrictions=permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination"
# 设置安全限制规则(与收件人规则类似,增强安全性)
postconf -e "smtpd_sasl_security_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination"

# === Dovecot 配置部分 ===
# 启用 IMAP、POP3、LMTP 协议
sed -i 's/^#protocols =.*/protocols = imap pop3 lmtp/' /etc/dovecot/dovecot.conf
# 允许 Dovecot 监听所有 IPv4 和 IPv6 地址
sed -i 's/^#listen =.*/listen = *, ::/' /etc/dovecot/dovecot.conf
# 允许纯文本密码认证
sed -i 's/^#disable_plaintext_auth =.*/disable_plaintext_auth = no/' /etc/dovecot/conf.d/10-auth.conf
# 指定认证机制为明文(PLAIN)和登录(LOGIN)
sed -i 's/^auth_mechanisms =.*/auth_mechanisms = plain login/' /etc/dovecot/conf.d/10-auth.conf
# 设置邮件存储格式为 Maildir(邮件目录结构)
sed -i "s|^#mail_location =.*|mail_location = maildir:~/Maildir|" /etc/dovecot/conf.d/10-mail.conf
# 关闭 SSL/TLS 加密
sed -i "s/^ssl =.*/ssl = no/" /etc/dovecot/conf.d/10-ssl.conf
# 定义 POP3 UIDL 格式
sed -i "s/^#pop3_uidl_format =.*/pop3_uidl_format = %08Xu%08Xv/" /etc/dovecot/conf.d/20-pop3.conf
# 添加 POP3 客户端兼容性选项(解决 Outlook 等客户端兼容问题)
sed -i "s/^#pop3_client_workarounds =.*/pop3_client_workarounds = outlook-no-nuls oe-ns-eoh/" /etc/dovecot/conf.d/20-pop3.conf

systemctl restart postfix
systemctl enable dovecot --now

ss -tunlp | grep dove
tcp LISTEN 0 100 0.0.0.0:110 0.0.0.0:* users:(("dovecot",pid=18901,fd=21))
tcp LISTEN 0 100 0.0.0.0:143 0.0.0.0:* users:(("dovecot",pid=18901,fd=37))
tcp LISTEN 0 100 [::]:110 [::]:* users:(("dovecot",pid=18901,fd=22))
tcp LISTEN 0 100 [::]:143 [::]:* users:(("dovecot",pid=18901,fd=38))

#添加多用户
groupadd mailusers
useradd -g mailusers -s /sbin/nologin wangsheng
useradd -g mailusers -s /sbin/nologin xhyhuiying
echo 1 | passwd --stdin wangsheng
echo 1 | passwd --stdin xhyhuiying

通过foxmail登录与测试

使用一台windows10桥接到10.163.2.0/24网段上,并且设置网卡dns服务器为10.163.2.100

因为上面dovecot只定义了pop3的方式登录,所以imap和exchange方式都无法登录,使用pop3登录

发送邮件报错分析

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
tail -f /var/log/maillog
...
Apr 7 23:45:02 localhost postfix/smtpd[19861]: warning: SASL: Connect to private/auth failed: No such file or directory
Apr 7 23:45:02 localhost postfix/smtpd[19861]: fatal: no SASL authentication mechanisms
Apr 7 23:42:30 localhost dovecot[19840]: pop3-login: Login: user=<wangsheng>, method=PLAIN, rip=10.163.2.109, lip=10.163.2.106, mpid=19851, session=<NOZmJDwya8IKowJt>
Apr 7 23:42:30 localhost dovecot[19840]: pop3(wangsheng)<19851><NOZmJDwya8IKowJt>: Disconnected: Logged out top=0/0, retr=0/0, del=0/0, size=0
Apr 7 23:42:30 localhost dovecot[19840]: pop3-login: Login: user=<xhyhuiying>, method=PLAIN, rip=10.163.2.109, lip=10.163.2.106, mpid=19856, session=<w4xnJDwybMIKowJt>
Apr 7 23:42:30 localhost dovecot[19840]: pop3(xhyhuiying)<19856><w4xnJDwybMIKowJt>: Disconnected: Logged out top=0/0, retr=0/0, del=0/0, size=0
Apr 7 23:45:02 localhost dovecot[19840]: imap-login: Disconnected: Connection closed (disconnected before auth was ready, waited 0 secs): user=<>, rip=10.163.2.109, lip=10.163.2.106, session=<vkZ9LTwydcIKowJt>

可见pop3认证成功,但SMTP认证失败
原因是postfix去找Dovecot提供的认证套接字文件private/auth,结果发现没有

修复:
vim /etc/dovecot/conf.d/10-master.conf
service auth {
unix_listener /var/spool/postfix/private/auth {
mode = 0666 # 允许 Postfix 读写
user = postfix # 指定用户为 postfix
group = postfix # 指定组为 postfix
}
}

原文作者:王盛

原文链接:https://akemi.zj.cn/2025/04/08/Postfix/

发表日期:April 8th 2025, 1:39:00 pm

更新日期:April 8th 2025, 7:34:07 pm

版权声明:本文采用知识共享署名-非商业性使用 4.0 国际许可协议进行许可

CATALOG
  1. 1. 电子邮件基本概念
  2. 2. 使用postfix搭建MTA
    1. 2.1. 搭建dns服务器
    2. 2.2. postfix搭建SMTP邮件服务器
    3. 2.3. 测试发送邮件
  3. 3. 使用dovecot搭建MRA
  4. 4. 通过foxmail登录与测试
    1. 4.1. 发送邮件报错分析