环境准备

  • 虚拟机/云服务器
  • 操作系统 CentOS Linux 7.9 x86_64
  • 公网IP
  • 域名

域名解析配置

采用云服务器,登录管理网站添加域名

| 记录类型 | 主机记录 | 记录值 | 其他 |
| ——– | ——– | —————– | ——– |
| MX | @ | mail.makaixin.com | 优先级10 |
| A | mail | 150.158.36.171 | |

image-20240510180332609

邮件服务

安装软件

yum -y install postfix dovecot  cyrus-sasl

修改配置

postfix

1. 修改配置
vim /etc/postfix/main.cf
2. 配置内容
# 修改以下项,注释的解开,不要有重复项
# 设置为 mail.域名
myhostname = mail.amserv.top
# 设置为 域名
mydomain = amserv.top
# 往外发邮件的邮件域
myorigin = $mydomain
#监听的网卡
inet_interfaces = all
inet_protocols = all
#服务的对象
mydestination = $myhostname,$mydomain
#邮件存放的目录
home_mailbox = Maildir/

# 新添加以下配置
#--------自定义(下面可以复制粘贴到文件最后面,用于设置服务器验为主,第一行设置发送附件大小)
#message_size_limit = 100000
smtpd_sasl_auth_enable = yes
smtpd_sasl_security_options = noanonymous
mynetworks = 127.0.0.0/8
smtpd_recipient_restrictions = permit_mynetworks,permit_sasl_authenticated,reject_unauth_destination
3. 检查并启动postfix
# 检查
postfix check
# 启动
systemctl start postfix
# 开机自启动
systemctl enable postfix

dovecot

1. 修改配置
vim /etc/dovecot/dovecot.conf
2. 配置内容
# 注释的解开注释,不要有重复项
protocols = imap pop3 lmtp
listen = *, ::

#以下内容添加到文件最后
!include conf.d/10-auth.conf

ssl = no
disable_plaintext_auth = no
mail_location = maildir:~/Maildir
3. 启动
# 启动dovecot
systemctl start dovecot
# 开机自启动
systemctl enable dovecot

cyrus-sasl

1. 修改配置
vim /etc/sysconfig/saslauthd 
2. 配置内容
MECH=shadow
3. 启动
# 启动 saslauthd
systemctl start saslauthd
# 开机自启动
systemctl enable saslauthd

测试

防火墙

需要打开防火墙TCP 25、110、143端口,宝塔面板/服务器防火墙

# 添加防火墙规则,允许 25、110、143端口
firewall-cmd --permanent --zone=public --add-port=25/tcp
firewall-cmd --permanent --zone=public --add-port=110/tcp
firewall-cmd --permanent --zone=public --add-port=143/tcp
# 重载防火墙配置
firewall-cmd --reload
# 查看端口开放列表
firewall-cmd --list-ports

查看端口监听状态

# 查看端口监听状态 没有该命令使用 yum -y install net-tools
netstat -ntpl

安装mailx命令测试

yum -y install mailx

Windows下查看DNS解析

nslookup mail.makaixin.com

mailx 测试

echo '在Centos上搭建邮件服务器成功啦!!!' | mail -s '通知!' makaixindalao@163.com

日志查询

如发现没有收到邮件,可以查看日志,注意反垃圾邮件

tail -f /var/log/maillog

创建用户

添加用户

useradd mailuser
# 123456
passwd mailuser

安装 cyrus-sasl-plain (不安装可能导致无法发信)

yum install cyrus-sasl-plain

testsaslauthd -u mailuser -p '123456'

在foxmail上添加自建的邮箱

添加邮箱->其他邮箱->手动设置

  • 接收服务器类型:POP3

  • 邮件账号:用户名@域名

  • 密码:刚刚设置的123456

  • POP/SMTP 服务器:mail.域名

  • 勾选 “如服务器支持,用STARTTLS加密传输”