电子邮件基本概念
类型
名称
协议
核心功能
常见示例
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.conflocal-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" cat /etc/unbound/conf.d/forward.com.confserver: 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 postconf -d 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" postconf -e "mynetworks=0.0.0.0/0" postconf -e "relay_domains=mail.wangsheng.com,localhost.wangsheng.com,localhost,wangsheng.com" 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 yum -y install s-nail 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=<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) 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 postconf -e "smtpd_sasl_type=dovecot" postconf -e "smtpd_sasl_path=private/auth" postconf -e "smtpd_sasl_auth_enable=yes" 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" sed -i 's/^#protocols =.*/protocols = imap pop3 lmtp/' /etc/dovecot/dovecot.conf 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 sed -i 's/^auth_mechanisms =.*/auth_mechanisms = plain login/' /etc/dovecot/conf.d/10-auth.conf sed -i "s|^#mail_location =.*|mail_location = maildir:~/Maildir|" /etc/dovecot/conf.d/10-mail.conf sed -i "s/^ssl =.*/ssl = no/" /etc/dovecot/conf.d/10-ssl.conf sed -i "s/^#pop3_uidl_format =.*/pop3_uidl_format = %08Xu%08Xv/" /etc/dovecot/conf.d/20-pop3.conf 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 wangshengecho 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 user = postfix group = postfix } }