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 " 。

至此,再測試發送郵件則會成功發送。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章