Linux CentOS7 发送QQ邮件问题解决方案

环境
CentOS 7+
QQ企业邮箱: smtp.exmail.qq.com
#POP3/SMTP协议
接收邮件服务器:pop.exmail.qq.com ,使用SSL,端口号995
发送邮件服务器:smtp.exmail.qq.com ,使用SSL,端口号465
#海外用户可使用以下服务器
接收邮件服务器:hwpop.exmail.qq.com ,使用SSL,端口号995
发送邮件服务器:hwsmtp.exmail.qq.com ,使用SSL,端口号465




常用命令:
查看邮件日志:vi /var/log/maillog
配置邮件:vi /etc/mail.rc
编辑主机地址:vi /etc/hosts
发送邮件测试:echo "mail content" | mail -s "title" [email protected]
	     #echo "Hello" | mail -v -s "test" [email protected]



步骤
(1). 安装sendMail,  案例网上很多。
官方网站:http://caspian.dotconf.net/menu/Software/SendEmail/
下载地址:http://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1.56.tar.gz


安装方法参考如下:
tar –zxvf sendEmail-v1.56.tar.gz
cd sendEmail-v1.56
cp sendEmail /usr/local/bin
service sendmail start



具体参考:
1. Linux下sendmail发送邮件: http://blog.csdn.net/zhuying_linux/article/details/7091688
2.Centos下如何使用sendmail发送邮件: https://weibo.com/p/230418660f66300102x875


(2). 配置 /etc/mail.rc :
# mail config - 2017/12/04
set [email protected]                             #发送方邮件地址
set smtp=smtps://smtp.exmail.qq.com:465          #邮件服务器
set smtp-auth=login                              #验证方式/登录验证
set smtp-auth-user="[email protected]"                #发送方用户权限验证
set smtp-auth-password="xxxxxxxxxx"              #发送方验证密钥
set ssl-verify=ignore
set nss-config-dir=/root/.certs
#set nss-config-dir=/etc/pki/nssdb


说明:因实践过程中采用的是阿里云服务器,根据规则阿里云屏蔽了25端口,所以只能采用SSL / 465 发送邮件,注意smtp的配置格式:
set smtp=smtps://smtp.exmail.qq.com:465


(3).启动sendMail 并 重启服务器:reboot;   具体参考文章《 Centos下如何使用sendmail发送邮件》。
(4).可通过以下命令进行测试:
echo "mail content" | mail -s "title" [email protected]
#echo "Hello" | mail -v -s "test" [email protected]


过程:
在实践过程中遇到不少坑,记录过程如下,以供参考:

开始,笔者只配置了/etc/mail.rc,格式如下:
# mail config - 2017/12/04
set [email protected]                             
set smtp=smtp.exmail.qq.com                      
set smtp-auth=login                               
set smtp-auth-user="[email protected]"                 
set smtp-auth-password="xxxxxxxxxx"  


结果输入命令测试:
echo "mail content" | mail -s "title" [email protected]


出现Connection timeout异常:
com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.exmail.qq.com, 25; timeout -1;
  nested exception is:
        java.net.ConnectException: Connection timed out
        at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1961)
        at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:654)
        at javax.mail.Service.connect(Service.java:345)
        at javax.mail.Service.connect(Service.java:226)
        at com.gaigo.main.utils.MailUtils.sendMailNotice(MailUtils.java:94)
        at com.gaigo.business.service.impl.PayServiceImpl.sendSMSNotice(PayServiceImpl.java:841)
        at com.gaigo.business.service.impl.PayServiceImpl.sendTaskNotice(PayServiceImpl.java:667)


开始是以为25端口没开启的缘故,于是配置25端口:
 -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 25 -j ACCEPT


但是结果还是报异常。
结果,经查是因为阿里云屏蔽了25端口,于是直接改成SSL发送邮件,然后更具网友的文章更改配置:
# mail config - 2017/12/04
set [email protected]                            
set smtp=ssl://smtp.exmail.qq.com                     #增加了ssl://
set smtp-auth=login                               
set smtp-auth-user="[email protected]"                 
set smtp-auth-password="xxxxxxxxxx"    



于是出现了(sm-msp-queue says: unable to qualify my own domain name (xxx) — using short name)如下异常:
vi /var/log/maillog
#出现如下异常信息:
Dec  4 18:03:45 iZwz98pmxwulw6kd0n2g4dZ sendmail[5231]: My unqualified host name (iZwz98pmxwulw6kd0n2g4dZ) unknown; sleeping for retry
Dec  4 18:04:45 iZwz98pmxwulw6kd0n2g4dZ sendmail[5231]: unable to qualify my own domain name (iZwz98pmxwulw6kd0n2g4dZ) -- using short name
Dec  4 18:04:45 iZwz98pmxwulw6kd0n2g4dZ sendmail[5231]: alias database /etc/aliases rebuilt by root
Dec  4 18:04:45 iZwz98pmxwulw6kd0n2g4dZ sendmail[5231]: /etc/aliases: 76 aliases, longest 10 bytes, 771 bytes total
Dec  4 18:05:35 iZwz98pmxwulw6kd0n2g4dZ sendmail[5264]: My unqualified host name (iZwz98pmxwulw6kd0n2g4dZ) unknown; sleeping for retry
Dec  4 18:06:35 iZwz98pmxwulw6kd0n2g4dZ sendmail[5264]: unable to qualify my own domain name (iZwz98pmxwulw6kd0n2g4dZ) -- using short name
Dec  4 18:06:35 iZwz98pmxwulw6kd0n2g4dZ sendmail[5269]: starting daemon (8.14.7): SMTP+queueing@01:00:00
Dec  4 18:06:35 iZwz98pmxwulw6kd0n2g4dZ sm-msp-queue[5286]: My unqualified host name (iZwz98pmxwulw6kd0n2g4dZ) unknown; sleeping for retry
Dec  4 18:07:35 iZwz98pmxwulw6kd0n2g4dZ sm-msp-queue[5286]: unable to qualify my own domain name (iZwz98pmxwulw6kd0n2g4dZ) -- using short name
Dec  4 18:07:35 iZwz98pmxwulw6kd0n2g4dZ sm-msp-queue[5288]: starting daemon (8.14.7): queueing@01:00:00
Dec  4 18:25:40 iZwz98pmxwulw6kd0n2g4dZ sendmail[5338]: My unqualified host name (iZwz98pmxwulw6kd0n2g4dZ) unknown; sleeping for retry


经查,此处原因说是需要更改/etc/hosts配置文件,更改如下:
vi /etc/hosts
#原文件内容:
127.0.0.1 localhost
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
10.30.120.5 GaigoServer
#修改后文件内容:
127.0.0.1 localhost
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
10.30.120.5 GaigoServer gaigo.cn     #增加了域名gaigo.cn格式配置


 
   
 
   
 
   
 
   
 
   
 
  
至此,网友配置均能发送成功,但笔者依然发送不出去。于是既然是阿里服务器,就在其论坛查找ssl邮件配置方法,找到相关配置如下:
也可以参考文章:https://stackoverflow.com/questions/16799407/mailx-and-gmail-nss-config-dir
set smtp="smtps://smtp.mxhichina.com:465"
set smtp-auth=login
set smtp-auth-user="[email protected]"
set smtp-auth-password="Ni-De-Mi-Ma"
set ssl-verify=ignore
set nss-config-dir=/etc/pki/nssdb


于是笔者将/etc/mail.rc配置更改如下:
# mail config - 2017/12/04
set [email protected]                              
set smtp=smtps://smtp.exmail.qq.com:465           
set smtp-auth=login                               
set smtp-auth-user="[email protected]"                 
set smtp-auth-password="xxxxxxxxxx"               
set ssl-verify=ignore
set nss-config-dir=/etc/pki/nssdb


然后测试发送,果然发送成功! 但是依然有Error出现: Error in certificate: Peer's certificate issuer is not recognized.   


此问题可参考文章:
使用 Mailx 通过 SMTP 在 Centos 上发送邮件:https://www.wpzhiku.com/shi-yong-mailx-tong-guo-smtp-zai-centos-shang-fa-song-you-jian/
mailx及sendEmail的基本用法比较:http://blog.51cto.com/irow10/1812638
解决方案,运行以下命令生成一个证书即可。
mkdir -p /root/.certs/
echo -n | openssl s_client -connect smtp.qq.com:465 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > ~/.certs/qq.crt
certutil -A -n "GeoTrust SSL CA" -t "C,," -d ~/.certs -i ~/.certs/qq.crt
certutil -A -n "GeoTrust Global CA" -t "C,," -d ~/.certs -i ~/.certs/qq.crt
certutil -L -d /root/.certs


生成完成之后,修改 mail.rc 邮件配置,修改 nss-config-dir 为上面命令生成的 /root/.certs,保存即可。
[root@GaigoServer /]# cd /root/.certs
[root@GaigoServer .certs]# ll
total 80
-rw------- 1 root root 65536 Dec  6 14:44 cert8.db
-rw------- 1 root root 16384 Dec  6 14:44 key3.db
-rw-r--r-- 1 root root  2724 Dec  6 14:35 qq.crt
-rw------- 1 root root 16384 Dec  6 14:35 secmod.db
[root@GaigoServer .certs]# 


但是,即使有了证书依然出现“ Error in certificate: Peer's certificate issuer is not recognized. ”提示。证书并没有得到认可,需要再运行一下如下命令:
[root@GaigoServer .certs]# pwd
/root/.certs
[root@GaigoServer .certs]# ll
total 80
-rw------- 1 root root 65536 Dec  6 14:44 cert8.db
-rw------- 1 root root 16384 Dec  6 14:44 key3.db
-rw-r--r-- 1 root root  2724 Dec  6 14:35 qq.crt
-rw------- 1 root root 16384 Dec  6 14:35 secmod.db
[root@GaigoServer .certs]# certutil -A -n "GeoTrust SSL CA - G3" -t "Pu,Pu,Pu"  -d ./ -i qq.crt 


说明:先找到证书位置,在运行"certutil -A -n "GeoTrust SSL CA - G3" -t "Pu,Pu,Pu"  -d ./ -i qq.crt " 。

至此,再测试发送邮件则会成功发送。

你可能感兴趣的:(Linux/虚拟机)