电子邮件基本概念
类型
名称
协议
核心功能
常见示例
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.com2.mail.qq.com查找163.com的MX DNS记录,通过MDA将邮件relay到mail.163.com3.目标客户端从mail.163.com将邮件下载下来
使用postfix搭建MTA
搭建dns服务器123456789101112131415161718192021222324252627282930313233yum -y install unboundvim /etc/unbound/unbound.confinterface: 0.0.0.0access-control: 0.0.0.0/0 allow# 本地域配置cat /etc/unbound/local.d/wangsheng.com.conflocal-zone: "wangsheng.com." staticlocal-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"# 转发域配置,忽略DNSSECcat /etc/unbound/conf.d/forward.com.confserver:domain-insecure: "com."domain-insecure: "net."domain-insecure: "org."forward-zone:name: "."forward-addr: 114.114.114.114unbound-checkconfunbound-control-setupsystemctl enable unbound --nowhost mail.wangsheng.com.mail.wangsheng.com has address 10.163.1.106
postfix搭建SMTP邮件服务器1234567891011121314151617181920212223242526272829303132333435363738yum -y install postfixls /etc/postfix/access dynamicmaps.cf.d/ main.cf master.cf.proto relocatedcanonical generic main.cf.proto postfix-files transportdynamicmaps.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"# 设置邮件存储格式为 Maildirpostconf -e "home_mailbox=Maildir/"systemctl disable firewalld --nowsetenforce 0systemctl enable postfix --now
测试发送邮件1234567891011121314151617181920212223242526272829303132333435363738yum -y install mailx # centos7yum -y install s-nail # centos8及新版本echo "this is a test message from local" | s-nail -s "Test Mail" root@wangsheng.comecho "this is a test message from local" | mail -s "Test Mail" root@wangsheng.com看一眼日志:tail -f /var/log/maillogApr 7 18:20:28 mail-client postfix/smtp[16362]: 82B045C17: to=
使用dovecot搭建MRA使MUA可以通过MRA接受邮件
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051yum -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.confsystemctl restart postfixsystemctl enable dovecot --nowss -tunlp | grep dovetcp 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 mailusersuseradd -g mailusers -s /sbin/nologin wangshenguseradd -g mailusers -s /sbin/nologin xhyhuiyingecho 1 | passwd --stdin wangshengecho 1 | passwd --stdin xhyhuiying
通过foxmail登录与测试使用一台windows10桥接到10.163.2.0/24网段上,并且设置网卡dns服务器为10.163.2.100
因为上面dovecot只定义了pop3的方式登录,所以imap和exchange方式都无法登录,使用pop3登录
发送邮件报错分析
12345678910111213141516171819202122tail -f /var/log/maillog...Apr 7 23:45:02 localhost postfix/smtpd[19861]: warning: SASL: Connect to private/auth failed: No such file or directoryApr 7 23:45:02 localhost postfix/smtpd[19861]: fatal: no SASL authentication mechanismsApr 7 23:42:30 localhost dovecot[19840]: pop3-login: Login: user=