【mail郵件系統】linux上安裝部署sendmail郵件系統

sendmail是linux系統中一個郵箱系統,在系統中配置好sendmail就能夠直接使用它來發送郵箱。
sendmail的配置文件html

/etc/mail/sendmail.cf :Sendmail的主配置文件; /etc/mail/access :中繼訪問控制; /etc/mail/domaintable ;域名映射; /etc/mail/local-host-names ;本地主機別名; /etc/mail/mailertable :爲特定的域指定特殊的路由規則; /etc/mail/virtusertable :虛擬域配置。

中繼的配置:是指一臺服務器接受並傳遞源地址和目的地址都不是本服務器的郵件。linux

在兩個文件中進行設置:vim

/etc/mail/relay-domains
/etc/mail/access。

1、安裝軟件

[root@VM_0_10_centos ~]# yum -y install sendmail sendmail-cf

啓動saslauthd服務進行SMTP驗證(默認是安裝的,若是沒有,就手動安裝)centos

[root@VM_0_10_centos ~]# systemctl restart saslauthd

2、郵件服務配置

1)需關閉防火牆服務器

[root@VM_0_10_centos ~]# systemctl stopfirewalld
[root@VM_0_10_centos ~]# systemctl status firewalld

2)配置Senmail的SMTP認證
將下面兩行內容前面的dnl去掉。在sendmail文件中,dnl表示該行爲註釋行,是無效的,所以經過去除行首的dnl字符串能夠開啓相應的設置行。網絡

[root@VM_0_10_centos ~]# vim /etc/mail/sendmail.mc 
TRUST_AUTH_MECH(`EXTERNAL DIGEST-MD5 CRAM-MD5 LOGIN PLAIN')dnl
define(`confAUTH_MECHANISMS', `EXTERNAL GSSAPI DIGEST-MD5 CRAM-MD5 LOGIN PLAIN')dnl

3) 設置Sendmail服務的網絡訪問權限(若是是直接本機調用,能夠不用操做,採用默認的127.0.0.1。不過最後仍是改爲0.0.0.0)
將127.0.0.1改成0.0.0.0,意思是任何主機均可以訪問Sendmail服務。
若是僅讓某一個網段可以訪問到Sendmail服務,將127.0.0.1改成形如192.168.1.0/24的一個特定網段地址。dom

[root@VM_0_10_centos ~]# vim /etc/mail/sendmail.mc
DAEMON_OPTIONS(`Port=smtp,Addr=0.0.0.0, Name=MTA')dnl

4)生成配置文件
Sendmail的配置文件由m4來生成,m4工具在sendmail-cf包中。若是系統沒法識別m4命令,說明sendmail-cf軟件包沒有安裝工具

# 修改以前先備份
[root@VM_0_10_centos ~]# cp -r /etc/mail/sendmail.cf /etc/mail/sendmail.cf.bak
[root@VM_0_10_centos ~]#  m4 /etc/mail/sendmail.mc > /etc/mail/sendmail.cf

5)啓動服務(若是發現sendmail dead but subsys locked,那就執行"service postfix status"查看postfix是否默認開啓了,若是開啓的話,就關閉postfix,而後再啓動或重啓sendmail服務便可。)post

[root@VM_0_10_centos ~]# systemctl restart sendmail
[root@VM_0_10_centos ~]# systemctl restart saslauthd
[root@VM_0_10_centos ~]# systemctl status postfix(若是是開啓的,將status改成stop便可)

將服務加入到開機自啓測試

[root@VM_0_10_centos ~]# systemctl enable sendmail.service
[root@VM_0_10_centos ~]# systemctl enable saslauthd.service
Created symlink from /etc/systemd/system/multi-user.target.wants/saslauthd.service to /usr/lib/systemd/system/saslauthd.se
rvice.
[root@VM_0_10_centos ~]# systemctl list-unit-files | grep sendmail
sendmail.service                              enabled 
[root@VM_0_10_centos ~]# systemctl list-unit-files | grep saslauthd
saslauthd.service                             enabled

3、測試發送郵箱

1)第一種方式:安裝sendmail便可使用。

# 默認好像已經安裝
[root@VM_0_10_centos ~]# yum -y install mailx

PS:建立一個郵件內容文件,而後發郵件(注意-s參數後的郵件標題要用單引號,不能使用雙引號,不然發郵件會失敗!)

[root@VM_0_10_centos ~]# echo 'This is test mail' > /root/testmail.txt
[root@VM_0_10_centos ~]# cat /root/testmail.txt 
This is test mail
[root@VM_0_10_centos ~]# mail -s 'Test Mail' qqMail@qq.com < /root/testmail.txt

PS:我這邊使用的是qq.com,發送郵件並未接收到,多是須要在qq郵箱開啓受權碼。而後在服務器上進行配置才行,操做以下

先開啓受權碼:

 往下滑動,開啓簽名兩個服務表示已經開啓了smtp功能。點擊生 成受權碼 按照提示繼續操做就能夠獲取受權碼了

發送以後點擊我已發送,生成受權碼便可。

編輯配置文件:

[root@VM_0_10_centos ~]# vi /etc/mail.rc
set from=qqMail@qq.com      # 對方收到郵件時顯示的發件人
set smtp=smtps://smtp.qq.com:465        # 第三方發郵件的smtp服務器地址
set smtp-auth-user=qqMail@qq.com   # 第三方發郵件的用戶名
set smtp-auth-password=*********** # 受權碼
set ssl-verify=ignore
set nss-config-dir=/root/.certs/
set smtp-auth=login     # SMTP的認證方式
set smtp-use-starttls

加入上面配置保存之後,重啓郵件服務:

# systemctl restart sendmail
# systemctl restart saslauthd

運行如下命令測試發送郵件,若是沒有意外,咱們的郵箱很快就能收到從服務器發過來的測試郵件了。

在這裏報錯:

# 建立/root/.certs目錄
# mkdir -p /root/.certs
[root@VM_0_10_centos .certs]# echo -n | openssl s_client -connect smtp.qq.com:465 | sed -ne '/-BEGIN CERTIFICATE-/,
/-END CERTIFICATE-/p' >qq.crt
depth=2 C = US, O = DigiCert Inc, OU = www.digicert.com, CN = DigiCert Global Root CA verify return:1 depth=1 C = US, O = DigiCert Inc, OU = www.digicert.com, CN = Secure Site CA G2 verify return:1 depth=0 C = CN, ST = Guangdong, L = Shenzhen, O = Tencent Technology (Shenzhen) Company Limited, OU = \E4\BC\81\E4\B8\9A\E 5\BE\AE\E4\BF\A1\E4\BA\A7\E5\93\81\E9\83\A8, CN = *.exmail.qq.comverify return:1 DONE [root@VM_0_10_centos .certs]# certutil -A -n "GeoTrust SSL CA" -t "C,," -d /root/.certs/ -i qq.crt [root@VM_0_10_centos .certs]# certutil -A -n "GeoTrust Global CA" -t "C,," -d /root/.certs/ -i qq.crt [root@VM_0_10_centos .certs]# certutil -L -d /root/.certs/ Certificate Nickname Trust Attributes SSL,S/MIME,JAR/XPI GeoTrust SSL CA C,, [root@VM_0_10_centos .certs]# certutil -A -n "GeoTrust SSL CA - G3" -t "Pu,Pu,Pu" -d ./ -i qq.crt Notice: Trust flag u is set automatically if the private key is present.

這樣配置仍是沒有解決,查看日誌/var/log/mailog(多是主機沒有解析到的緣由)

解決:設置/etc/hosts添加以下內容,而後保存退出,重啓服務

好吧,仍是沒解決

後面按照這個博客的試了下,也出現了問題https://blog.csdn.net/qq_43277505/article/details/85337072

意思是:來自地址的501郵件必須與受權用戶相同,查看mail.rc配配置文件發現是from後面的qq.com寫成了163.com

 

繞了這麼大圈終於解決了,開森

 

[root@VM_0_10_centos .certs]# echo "測試阿里雲465smtp端口發信是否ok!,若是收到了此封郵件則說明ok了!"  | mail -v -s 'test
' **@qq.comResolving host smtp.qq.com . . . done.
Connecting to 14.18.245.164:465 . . . connected.
Comparing DNS name: "pop.qq.com"
Comparing DNS name: "dav.qq.com"
Comparing DNS name: "ex.qq.com"
Comparing DNS name: "smtp.qq.com"
SSL parameters: cipher=AES-128, keysize=128, secretkeysize=128,
issuer=CN=Secure Site CA G2,OU=www.digicert.com,O=DigiCert Inc,C=US
subject=CN=pop.qq.com,OU=R&D,O=Tencent Technology (Shenzhen) Company Limited,L=Shenzhen,ST=Guangdong,C=CN
220 smtp.qq.com Esmtp QQ Mail Server
>>> EHLO VM_0_10_centos
250-smtp.qq.com
250-PIPELINING
250-SIZE 73400320
250-AUTH LOGIN PLAIN
250-AUTH=LOGIN
250-MAILCOMPRESS
250 8BITMIME
>>> AUTH LOGIN
334 VXNlcm5hbWU6
>>> MjM2MDQxNTg3MUBxcS5jb20=
334 UGFzc3dvcmQ6
>>> ZndkY29va2JvdWV5ZWJhaQ==
235 Authentication successful
>>> MAIL FROM:<**@qq.com>
250 Ok
>>> RCPT TO:<**@qq.com>
250 Ok
>>> DATA
354 End data with <CR><LF>.<CR><LF>
>>> .
250 Ok: queued as 
>>> QUIT
221 Bye

 

 

 

 

 

 

參考博客:

http://www.javashuo.com/article/p-vgzwqdau-bu.html

https://blog.csdn.net/qq_43277505/article/details/85337072(推薦)

相關文章
相關標籤/搜索