1. 前期准備:
主機:CentOS release 7.6.1810 (Core) #安裝時選擇郵件服務器
IP:192.168.71.108 #示例
本地yum源 #因為是內網,所以建議用鏡像搭建本地yum源
2. 基礎環境配置
關閉selinux:
sed –I ‘s/enforcing/disabled/g’ /etc/selinux/config

防火牆等配置:
firewall-cmd –add-port=’110/tcp’ –permanent
firewall-cmd –add-port=’25/tcp’ –permanent

或直接關閉防火牆,因為是內網,也不會有安全問題
systemctl start firewalld.service
主機名配置:

重啟系統以使上述配置生效
shutdown -r now
3. Postfix安裝與配置
3.1. 安裝postfix
因為安裝centos時選擇的是郵件系統,所以檢查是否自帶postfix服務,如果沒帶則可通過本地yum源安裝:yum -y insatll postfix --nogpgcheck

3.2. 配置postfix
[root@mail ~]# vim /etc/postfix/main.cf


- myhostname =mail.yzp.com #定義主機名
- mydomain =yzp.com #定義域名
- myorigin =$mydomain #定義組織域
- 我們需要在mydestination的值后面添加 $mydomain ,
- 如果不添加的話,收件人的地址會有問題,如果添加后,用戶發送郵件直接會是xx@yzp.com
- 取消注釋 #local_recipient_maps =
3.3. 啟動和檢查postfix
[root@mail ~]# systemctl start postfix.service
[root@mail ~]# systemctl enable postfix.service
[root@mail ~]# netstat -nltp

4. dovecot安裝與配置
4.1. 安裝dovecot
[root@mail ~]# yum -y install dovecot --nogpgcheck

4.2. 配置dovecot
接下來我們查看dovecot的默認配置文件
[root@mail ~]# cat /etc/dovecot/dovecot.conf
我們看見以下include可以包含的配置文件路徑,在該路徑下的*.conf文件都會生效

4.2.1. vim /etc/dovecot/dovecot.conf啟用協議以下協議及監聽
protocols = imap pop3 lmtp
#啟用端口監聽
listen = *,::
4.2.2. vim /etc/dovecot/conf.d/10-auth.conf設置如下參數


vim /etc/dovecot/conf.d/10-auth.conf
disable_plaintext_auth = no
auth_mechanisms = plain login
!include auth-system.conf.ext
4.2.3. vim /etc/dovecot/conf.d/10-ssl.conf禁用ssl認證

vim /etc/dovecot/conf.d/10-ssl.conf
ssl = no
4.2.4. vim /etc/dovecot/conf.d/10-logging.conf啟用dovecot的日志

vim /etc/dovecot/conf.d/10-logging.conf
添加
info_log_path = /var/log/dovecot_info.log
debug_log_path = /var/log/dovecot_debug.log
4.3. 啟動和檢查dovecot

[root@mail ~]# systemctl start dovecot.service
[root@mail ~]# systemctl enable dovecot.service
[root@mail ~]# netstat -nltp | grep dovecot
5. 創建測試賬戶與測試郵件收發通信
5.1. 創建兩個測試賬戶

[root@mail ~]# useradd test1
[root@mail ~]# passwd test1
[root@mail ~]# useradd test2
[root@mail ~]# passwd test2
5.2. 測試110端口:收郵件端口
[root@mail ~]# telnet 192.168.71.108 110
Trying 192.168.71.108...
Connected to 192.168.71.108.
Escape character is '^]'.
+OK [XCLIENT] Dovecot ready.
user test1
+OK
pass 111111
+OK Logged in.
-ERR [SYS/PERM] Permission denied
Connection closed by foreign host.
我們查看log
[root@mail ~]# cd /var/log/
[root@mail log]# tailf maillog

我們切換到剛才的用戶:
[test1@mail ~]$ su - test1
密碼:
[test2@mail ~]$ mkdir -p ~/mail/.imap/INBOX
[test1@mail ~]$ su - test2
密碼:
[test2@mail ~]$ mkdir -p ~/mail/.imap/INBOX

再次測試110端口,發現測試通過了
[root@mail ~]# telnet 192.168.71.108 110
Trying 192.168.71.108...
Connected to 192.168.71.108.
Escape character is '^]'.
+OK [XCLIENT] Dovecot ready.
user test1
+OK
pass 111111
+OK Logged in.
quit
+OK Logging out.
Connection closed by foreign host.
5.3. 測試25端口:發郵件端口
[root@mail ~]# telnet 192.168.71.108 25
Trying 192.168.71.108...
Connected to 192.168.71.108.
Escape character is '^]'.
220 mail.yzp.com ESMTP unkown
mail from:test1@yzp.com
250 2.1.0 Ok
rcpt to:test2@yzp.com
250 2.1.5 Ok
data
354 End data with <CR><LF>.<CR><LF>
.
250 2.0.0 Ok: queued as 338CE61533C9
quit
221 2.0.0 Bye
Connection closed by foreign host.
5.4. 測試上述查收上述test1發給test2的郵件

6. 同局域網內的windows電腦配置foxmail測試郵件收發
6.1. 配置新建賬號


6.2. 互相收發郵件


收發測試通過,至此,環境搭建通過。
7. 檢查郵件存儲位置
[root@mail postfix]# cat /etc/dovecot/conf.d/10-mail.conf | grep mail_location
# path given in the mail_location setting.
# mail_location = maildir:~/Maildir
mail_location = mbox:~/mail:INBOX=/var/mail/%u
# mail_location = mbox:/var/mail/%d/%1n/%n:INDEX=/var/indexes/%d/%1n/%n
#mail_location =
# mail_location, which is also the default for it.

[root@mail postfix]# cd /var/mail/

