CentOS 7中通过SMTP发送邮件 – mailx

yum install mailx

rpm -qa | grep mailx
mailx-12.5-19.el7.x86_64

rpm -ql mailx-12.5-19.el7.x86_64
/bin/mail
/bin/mailx
/etc/mail.rc
/usr/bin/Mail
/usr/bin/nail

配置文件是/etc/mail.rc,修改:

vi /etc/mail.rc

set from=xx
set smtp=smtp.mxhichina.com
set smtp-auth-user=xx
set smtp-auth-password=xx
set smtp-auth=login

发送邮件:

echo 'xxxxx' | mailx -s "hello" xxx@xx.com

以上配置默认链接25端口,非安全,如果需要链接安全端口,需要修改:

set from=xx
set smtp=smtps://smtp.mxhichina.com:465
set smtp-auth-user=xx
set smtp-auth-password=xx
set smtp-auth=login
set ssl-verify=ignore
set nss-config-dir=/etc/pki/nssdb

发送邮件报错:
Error in certificate: Peer’s certificate issuer has been marked as not trusted by the.
但是邮件可以正确发送。大体上就是证书签发人被标记为未授信。

那么换一种方式,把证书取回来:

mkdir -p /root/.certs/
echo -n | openssl s_client -connect smtp.mxhichina.com:465 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > ~/.certs/mxhichina.crt
certutil -A -n "GeoTrust Global CA" -t "C,," -d ~/.certs -i ~/.certs/mxhichina.crt
certutil -L -d /root/.certs

配置改为:

set from=xx
set smtp=smtps://smtp.mxhichina.com:465
set smtp-auth-user=xx
set smtp-auth-password=xx
set smtp-auth=login
set ssl-verify=ignore
set nss-config-dir=/root/.certs

还是提示:Error in certificate: Peer’s certificate issuer has been marked as not trusted by the. 不过邮件已经发送出去。

继续做如下修改:

certutil -A -n "GeoTrust SSL CA - G3" -t "Pu,Pu,Pu" -d ~/.certs -i ~/.certs/mxhichina.crt

Notice: Trust flag u is set automatically if the private key is present.

提示消失,邮件也发送成功。

注:其它邮件服务商,取证书的方式类似。

QQ邮箱:

echo -n | openssl s_client -connect smtp.exmail.qq.com:465 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > ~/.certs/exmail.crt

certutil -A -n "GeoTrust Global CA" -t "C,," -d ~/.certs -i ~/.certs/exmail.crt

certutil -L -d /root/.certs

certutil -A -n "GeoTrust SSL CA - G3" -t "Pu,Pu,Pu" -d ~/.certs -i ~/.certs/exmail.crt