利用 CentOS 7.6 发送邮件
今天摸索了一下用linux发电子邮件,网上查找了许多资料,终于成功了,做个笔记留着以后忘记了看。
1.我用的是阿里云的云服务器 CentOS 7.6 64位,因为阿里云把25端口封了,所以这里使用TSL方式,绕过25端口发送邮件;
2.首先输入以下命令安装mailx服务
yum install mailx -y
3.接着是获取证书,这里使用的是qq邮箱,如果用163邮箱,只需把第二句命令中的smtp.qq.com:465 换为smtp.163.com:465即可
[root@iZ3xc1s1h8mye7Z ~]# mkdir .certs
[root@iZ3xc1s1h8mye7Z ~]# echo -n | openssl s_client -connect smtp.qq.com:465 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > /root/.certs/qq.crt
[root@iZ3xc1s1h8mye7Z ~]# certutil -A -n "GeoTrust SSL CA" -t "C,," -d /root/.certs -i /root/.certs/qq.crt
[root@iZ3xc1s1h8mye7Z ~]# certutil -A -n "GeoTrust Global CA" -t "C,," -d /root/.certs -i /root/.certs/qq.crt
[root@iZ3xc1s1h8mye7Z ~]# certutil -A -n "GeoTrust SSL CA - G3" -t "Pu,Pu,Pu" -d /root/.certs/./ -i /root/.certs/qq.crt
Notice: Trust flag u is set automatically if the private key is present.
[root@iZ3xc1s1h8mye7Z ~]# ls /root/.certs/
qq.crt cert8.db key3.db secmod.db
[root@iZ3xc1s1h8mye7Z ~]# certutil -L -d /root/.certs
Certificate Nickname Trust Attributes
SSL,S/MIME,JAR/XPI
4.输入vim /etc/mail.rc命令,配置mail.rc文件
set from=xxxooo@qq.com #发邮件的qq号
set smtp=smtps://smtp.qq.com:465 #邮件服务器
set smtp-auth-user=xxxooo@qq.com #获得授权码的邮箱
set smtp-auth-password=你的授权码
set smtp-auth=login #默认login即可
set ssl-verify=ignore #ssl认证方式
set nss-config-dir=/root/.certs #证书所在目录
5.输入命令进行测试,测试结果如图所示(message是我创建的一个文件,里面是发送的内容)
[root@iZ3xc1s1h8mye7Z ~]# mailx -s "邮箱测试" xxxxx@qq.com < message
6.获取QQ邮箱授权码的操作如下
- 进入QQ邮箱,点击左上角的设置,再点击账户
- 开启SMTP服务,点击生成授权码,随后验证手机即可获取授权码