在共有云上目前都是封锁25端口的,也就是说想要在服务器上搭建SMTP是不可能的,除非申请解封25端口。
但是一般情况为了方便使用,可以直接选择第三方的SMTP服务器,如163,qq等等,省去自建的麻烦。
操作如下:
- 进入云服务器,检测是否安装mailx,如果没有则自行安装。
[root@VM-0-2-centos .certs]# rpm -qa | grep mailx
libreport-plugin-mailx-2.1.11-53.el7.centos.x86_64
mailx-12.5-19.el7.x86_64
- 这里我使用的是163邮箱的SMTP服务,进入配置文件,在最后写入配置
set from=xxxxxxx@163.com ##邮件发送者
set smtp=smtps://smtp.163.com:465 ##邮件服务器地址和端口
set ssl-verify=ignore ##忽略ssl认证
set nss-config-dir=/root/.certs ##证书存放路径
set smtp-auth-user=xxxxxx@163.com ##登录SMTP的用户名
set smtp-auth-password=xxxxxx ##登录SMTP的密码,这里需要使用授权码
set smtp-auth=login #SMTP的认证方式
163邮箱的授权码获取方式
- 获取证书
mkdir -p /root/.certs/ ##创建证书目录
cd /root/.certs
echo -n | openssl s_client -connect smtp.163.com:465 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > ~/.certs/163.crt
certutil -A -n “GeoTrust SSL CA” -t “C,” -d ~/.certs -i ~/.certs/163.crt
certutil -A -n “GeoTrust Global CA” -t “C,” -d ~/.certs -i ~/.certs/163.crt
certutil -A -n “GeoTrust SSL CA - G3” -t “Pu,Pu,Pu” -d ~/.certs/./ -i 163.crtchmod 777 * /root/.certs
[root@VM-0-2-centos .certs]# ll
total 64
-rwxrwxrwx 1 root root 2415 Jan 27 14:01 163.crt
-rwxrwxrwx 1 root root 65536 Jan 27 13:54 cert8.db
-rwxrwxrwx 1 root root 16384 Jan 27 13:54 key3.db
-rwxrwxrwx 1 root root 16384 Jan 27 13:54 secmod.dbcertutil -L -d /root/.certs ##列出目录下证书
- 测试邮件发送
echo "456" |mail -s "test" XXXX@qq.com
如果发送未收到,检查证书获取是否有问题,重新获取尝试。
如果出现报错如:Error in certificate: Peer’s certificate issuer is not recognized. 再执行一次 “certutil -A -n “GeoTrust SSL CA - G3” -t “Pu,Pu,Pu” -d ./ -i 163.crt”
如果发送邮件被扔到垃圾箱或还是无法收到,可以在收件人邮箱添加白名单测试。