配置Linux服务器从第三方 SMTP 服务器外发邮件

配置Linux服务器从第三方 SMTP 服务器外发邮件

1、确保postfix服务运行

# systemctl status postfix

2、安装mailx

# yum install -y mailx

3、配置smtp服务器

    修改/etc/mail.rc文件,在文件中添加以下内容
# vim /etc/mail.rc
set from=user_sunli@sina.com
set smtp=smtp.sina.com
set smtp-port=25
set smtp-auth-user=user_sunli@sina.com
set smtp-auth-password=xxxxxxxxxxxxx#授权码
set smtp-auth=login
 或者
set from=user_sunli@sina.com
set smtp=smtps://smtp.sina.com:465
set smtp-auth-user=user_sunli@sina.com
set smtp-auth-password=xxxxxxxxxxxxx#授权码
set smtp-auth=login
set ssl-verify=ignore 
set nss-config-dir=/root/.certs

4、测试

# echo "邮件内容" |mail -s "邮件标题" 公网邮箱
# echo "hello" |mail -s "hehehe" xxxxxx@qq.com
 如果是用的smtp-port=25和云服务器(阿里云/腾讯云)需要先去申请开通25端口(默认禁止),并且开放安全组中的25端口
 
 
 

 配置邮箱可能遇到的问题:

1).端口,sina邮箱smtp端口为25, 其他邮箱的端口需要查询
2).需要在邮箱设置中开启smtp服务
 
 

 

扩展(使用465端口不需要去申请开通25端口,但需要SSL)

以163邮箱为例,使用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                    ####向163请求证书
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                         ####列出目录下证书

二、配置发件人

输入命令: vim /etc/mail.rc
按如下图配置完成

三、测试

# echo “test” | mail -s “zabbix” xxxxxxx@qq.com
登陆收件人邮箱查看
看似成功但是linux中报错:证书不被信任

四、解决最后一个问题-----证书不被信任

按以下命令依次输入
# cd /root/.certs/
# ll
certutil -A -n "GeoTrust SSL CA - G3" -t "Pu,Pu,Pu" -d ./ -i 163.crt
成功标志:
Notice: Trust flag u is set automatically if the private key is present.
 
posted @ 2021-01-02 09:53  梦里花落知多少sl  阅读(979)  评论(0编辑  收藏  举报