安装MAILX
sudo apt-get install heirloom-mailx
配置文件
编辑Ubuntu中 etc/s-nail.rc //其他系统中可能叫nail.rc 或 mail.rc
在最后添加以下信息:
set from=USER@qq.com //发信人邮箱
set smtp=smtps://smtp.qq.com:465 //发信人邮箱的SMTP地址
set smtp-auth-user=USER //发信人邮箱登陆账号
set smtp-auth-password=PASSWORD //发信人邮箱系统提供的授权码
set smtp-auth=login
//授权码可以在邮箱中开启SMTP的地方获取
测试
echo"邮件内容" | mail -vs "邮件标题" 发送邮件的邮箱
-vs可以查看详细过程信息
注意!! 如果你是用的是阿里云的服务器,或者在connecting to XXXX 什么的时候一直连不上的话,可能是25端口被封,得改用465端口,所以set smtp那个地方才这么写
如果上述还不行,尝试如下:
(转自 https://www.cnblogs.com/along21/p/9849645.html)
1、关闭其它的邮件工具
[root@along ~]# systemctl stop sendmail
[root@along ~]# systemctl stop postfix
2、安装mailx
[root@along ~]# yum install mailx
3、开启smtp
在任何邮箱平台中开启smtp,开启后会得到一个授权码,这个授权码就代替了密码(自行去邮箱开启)。我使用的是163邮箱
4、请求数字证书(这里用的163邮箱,所以向163请求证书)
[root@along ~]# mkdir .certs
[root@along ~]# echo -n | openssl s_client -connect smtp.163.com:465 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > /root/.certs/163.crt
[root@along ~]# certutil -A -n "GeoTrust SSL CA" -t "C,," -d /root/.certs -i /root/.certs/163.crt
[root@along ~]# certutil -A -n "GeoTrust Global CA" -t "C,," -d /root/.certs -i /root/.certs/163.crt
[root@along ~]# certutil -A -n "GeoTrust SSL CA - G3" -t "Pu,Pu,Pu" -d /root/.certs/./ -i /root/.certs/163.crt
Notice: Trust flag u is set automatically if the private key is present.
[root@along ~]# ls /root/.certs/
163.crt cert8.db key3.db secmod.db
[root@along ~]# certutil -L -d /root/.certs
Certificate Nickname Trust Attributes
SSL,S/MIME,JAR/XPI
GeoTrust SSL CA P,P,P
5、配置/etc/mail.rc
[root@along ~]# vim /etc/mail.rc
set from=xxx@163.com #之前设置好的邮箱地址
set smtp="smtps://smtp.163.com:465" #邮件服务器
set smtp-auth-user=xxx@163.com #之前设置好的邮箱地址
set smtp-auth-password=xxxx #授权码
set smtp-auth=login #默认login即可
set ssl-verify=ignore #ssl认证方式
set nss-config-dir=/root/.certs #证书所在目录
6、发送邮件测试
[root@along ~]# echo "邮件正文" | mail -s "邮件主题" xxx@qq.com
邮件发送成功