1.查看是否已安装邮件服务 rpm -qa|grep mail
2. 如果没有mailx,则安装
yum install -y mailx
3.配置 cat /etc/mail.rc
set from=xxx@qq.com
set smtp=smtp.qq.com
set smtp-auth-user=xxx@qq.com
set smtp-auth-password=xxxx
set smtp-auth=login
如图:
注:smtp-auth-password 是授权密码,不是邮箱密码。设置如下图,qq邮箱账户设置,生成授权码,然后设置smtp-auth-password
4.发送邮件
mailx -s "centos7测试邮件" xxx@qq.com < ~/test.txt
echo 'hello' | mail -s "Linux测试" xxx@qq.com
cat ~/test.txt | mail -s "邮件测试" xxx@qq.com
备注:阿里云的centos的服务器有时候这样设置是发送不成功的,因为阿里云服务器关闭了25端口,发送邮件才会显示链接超时,而且官方不允许打开该端口,而且大部分邮件都是通过25端口。解决方法是使用SSL下的465端口
mkdir -p /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 ###添加一个ssl证书到证书数据库中
certutil -A -n "GeoTrust Global CA" -t "C,," -d ~/.certs -i ~/.certs/163.crt ###添加一个Global证书到证书数据库中
certutil -L -d /root/.certs ###列出目录下的数字证书
cd /root/.certs/
certutil -A -n "GeoTrust SSL CA - G3" -t "Pu,Pu,Pu" -d ./ -i 163.crt ###解决证书不被信任问题
编辑配置:vim /etc/mail.rc
添加如下两行:
set ssl-verify=ignore
set nss-config-dir=/root/.certs