CentOS7搭建簡單的郵件服務器


郵件服務器

概述

郵件收、發服務器是分開的,也就是我們需要搭建一個郵件發送服務器和一個郵件收取服務器。
本文會搭建收、發兩個服務器,並用郵件客戶端(Foxmail)做測試。

協議

協議就是定義規則,這里是郵件協議,定義郵件收發的規則,了解規則有助於理解軟件的配置文件。
郵件發送協議 SMTP(Simple Mail Transfer Protocol),打開端口 25。
郵件收取協議 POP,打開端口 110;還有個常用郵件收取協議 IMOP,打開端口 143。

服務軟件

Postfix
Postfix 是實現 SMTP 協議的軟件,也叫做郵件發送服務器。

上面說的郵件客戶端將郵件扔給它,由它對郵件進行轉發,至於怎么轉發,SMTP 協議制定了規則,而 Postfix 負責具體事情,我們只需要修改 Postfix 配置文件要求它按照我們的想法去做。

Dovecot
Dovecot 實現了 POP 和 IMOP 協議,也叫做郵件收取服務器。如果只搭建了 Postfix 而沒有它,不好意思,你是收不到郵件的。

Sasl
Sasl登陸驗證服務,在下面的介紹可以看到 Postfix 作為郵件發送服務器,不能無限制的轉發任意郵件,應當只轉發它信任的發件人發送的郵件,這一點體現在 Postfix 的配置文件要配置它認為安全的主機(mynetworks 參數)。但這樣會顯得很麻煩,Sasl 通過其它方式也可以幫助 Postfix 完成信任郵件的認證。

設置域名

mail.52zt.info用A記錄解析到郵件服務器IP(后面的各個客戶端配置的域名都寫這個A記錄的),再把52zt.info用MX記錄解析到mail.52zt.info(這個是當遇到***@52zt.info時會解析到mail.52zt.info)。
測試端口telnet命令也要用mail.52zt.info,不能用mx記錄的52zt.info(用這個會解析到A記錄解析的52zt.info)。

安裝軟件

安裝軟件postfix、dovecot、cyrus-sasl

yum -y install postfix dovecot  cyrus-sasl

配置軟件

配置postfix

vi /etc/postfix/main.cf
#修改以下配置,注意下面的變量不要重復,如果發現與原來的變量重名,那就將原來的變量給注釋掉
#郵件服務器的主機名
myhostname = mail.52zt.info
#郵件域,@后面的域名
mydomain = 52zt.info
#往外發郵件的郵件域
myorigin = $mydomain
#監聽的網卡
inet_interfaces = all
inet_protocols = all
#服務的對象
mydestination = $myhostname,$mydomain
#郵件存放的目錄
home_mailbox = Maildir/

#新添加以下配置
#--------自定義(下面可以復制粘貼到文件最后面,用於設置服務器驗為主,第一行設置發送附件大小)
#message_size_limit = 100000
smtpd_sasl_auth_enable = yes
smtpd_sasl_security_options = noanonymous
mynetworks = 127.0.0.0/8
smtpd_recipient_restrictions = permit_mynetworks,permit_sasl_authenticated,reject_unauth_destination
  • smtpd_sasl_auth_enable = yes //開啟認證
  • smtpd_sasl_security_options = noanonymous //不允許匿名發信
  • mynetworks = 127.0.0.0/8//允許的網段,如果增加本機所在網段就會出現允許不驗證也能向外域發信
  • smtpd_recipient_restrictions = permit_mynetworks,permit_sasl_authenticated,reject_unauth_destination
  • 允許本地域以及認證成功的發信,拒絕認證失敗的發信

檢查並啟動postfix

postfix check  #修改保存后檢查配置文件是否有錯
systemctl start postfix  #開啟postfix服務,CentOS6用service postfix start
systemctl enable postfix  #設置postfix服務開機啟動,CentOS6用chkconfig postfix on

配置dovecot

vi /etc/dovecot/dovecot.conf
#修改以下配置
protocols = imap pop3 lmtp
listen = *, ::

#新添加以下配置
#-----------自定義------------
!include conf.d/10-auth.conf

ssl = no
disable_plaintext_auth = no
mail_location = maildir:~/Maildir

啟動dovecot

systemctl start dovecot    #CentOS6用service dovecot start
systemctl enable dovecot    #CentOS6用chkconfig dovecot on

配置cyrus-sasl

vi /etc/sasl2/smtpd.conf    #如果是空文件,需要自己添加
pwcheck_method: saslauthd
mech_list: plain login
log_level:3
vi /etc/sysconfig/saslauthd  #修改下面配置項(本地用戶認證)
MECH=shadow

啟動

systemctl start saslauthd     #CentOS6用service saslauthd start
systemctl enable saslauthd    #CentOS6用chkconfig saslauthd on

添加用戶

添加用戶,並將密碼設為123456

 useradd  autumn
 echo  123456 | passwd --stdin autumn

測試

yum -y install telnet-server telnet    #安裝telnet客戶端

測試發送

[root@mail ~]# telnet localhost 25
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
220 mail.52zt.info ESMTP Postfix
mail from:autumn@52zt.info
250 2.1.0 Ok
rcpt to:qy***@foxmail.com
250 2.1.5 Ok
data
354 End data with <CR><LF>.<CR><LF>
subject:這是主題
this is test mail
.
250 2.0.0 Ok: queued as 6224C10263A

登錄郵箱

[root@mail ~]# telnet localhost 110
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
+OK Dovecot ready.
user autumn
+OK
pass 密碼
+OK Logged in.


list #列表查看郵件
retr 1 #讀取編號為1的郵件
quit #退出郵箱

用mailx測試

安裝

yum  install  mailx -y

使用mailx發送郵件

echo '測試郵件內容' | mail -s '測試主題!' qy***@foxmail.com

郵箱客戶端配置

Outlook配置

收郵件延遲較高,不推薦使用

如果出現

服務器錯誤: '554 5.7.1 Relay access denied'

1,打開outlook,點擊 “文件”“信息”bai,進入 “賬戶設置”。
2,雙擊賬戶,進入更改賬戶。
3,點擊 “其他設置”,選擇發送服務器選項卡,勾選我的發送服務器要求驗證(如果忘記勾選,只能收到郵件卻不能發送郵件)

Foxmail配置

選中設置->賬號->定時收取郵件,設置好每隔多少分鍾拉取郵件.推薦使用Foxmail.

出現問題

在起好了服務,開放了防火牆端口,設置了安全組的情況下。telnet localhost 25端口通,telnet 域名 25不通,是因為服務監聽ip的問題

vi /etc/postfix/main.cf

inet_interfaces=localhost 注釋掉這段,上面寫了all,沒注意這里還有個localhost

參考:
https://www.qcmoke.site/devops/mail.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM