Linux中用postfix搭建郵件服務器實戰詳解


Linux中用postfix搭建郵件服務器實戰詳解

postfix是Wietse Venema在IBM的GPL協議之下開發的MTA(郵件傳輸代理)軟件。
Postfix試圖更快、更容易管理、更安全,同時還與sendmail保持足夠的兼容性,因此它是免費的。
postfix的產生是為了替代傳統的sendmail。相較於sendmail,postfix在速度,性能和穩定性上都更勝一籌。
現在主流郵件服務都在采用postfix. 當需要一個輕量級的的郵件服務器時,postfix也是一種選擇。

常見的郵件服務器軟件:
sendmail:性能好,設置復雜,適合老手
qmail:體積小260+k ,模塊化。需要做二次開發,適合對郵件性能有要求的
postfix:前身是sendmail,postfix原本是sendmail里面的一個模塊,紅帽6默認安裝好postfix
zmailer:近幾年才出來的郵件
coremail:國內做的最好的商業平台,運行在linux上

---------------------
Linux搭建SMTP發送郵件服務器

1.關掉sendmail相關的所有服務,最好是直接卸載sendmail.
# service sendmail stop
# chkconfig sendmail off
# rpm -qa | grep sendmail | xargs rpm -e

2.安裝postfix.
查看是否安裝
# rpm -qa | grep postifx
沒有安裝的話,可以手動安裝。
# yum -y install postfix
檢查mail
# rpm -qa | grep mail
如果沒安裝就運行該命令
# yum -y install mailx

3.安裝完成后,修改配置文件:/etc/postfix/main.cfg
修改postfix配置文件
文件位置:/etc/postfix/main.cf
修改以下參數,將xxx.com換成你的域名,如果參數前面有#注釋,記得去掉
myhostname = mail.xxx.com
mydomain = xxx.com
myorigin = $mydomain
inet_interfaces = all
inet_protocols = ipv4
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
home_mailbox = Maildir/

4.修改完成后重啟postifx:
# service postfix restart
---------------------
myhostname = sample.test.com  ← 設置系統的主機名
mydomain = test.com  ← 設置域名(我們將讓此處設置將成為E-mail地址“@”后面的部分)
myorigin = $mydomain  ← 將發信地址“@”后面的部分設置為域名(非系統主機名)
inet_interfaces = all  ← 接受來自所有網絡的請求
inet_protocols = ipv4 ← ipv4要用小寫的,注釋里大寫的是錯的
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain  ← 指定發給本地郵件的域名
home_mailbox = Maildir/  ← 指定用戶郵箱目錄

郵箱投遞方式有兩種:一種是Mailbox方式,即同一個用戶的所有郵件內容存儲為單個文件,通常保存在/var/spool/mail/目錄下文件名與用戶名相同(Postfix默認使用);
第二種是Maildir方式,即使用目錄結構來存儲用戶的郵件內容每一個用戶使用一個文件夾,每封郵件都作為一個獨立的文件存放。Maildir方式的存取速度和效率要好一些對於管理郵件內容頁更加方便。

5.設置域名DNS
在郵件發送過程中,需要把郵件地址的domain地址轉化成IP地址,再去發送給對應的收件人。(DNS中的A記錄和MX記錄)

如何添加DNS server?
vim /etc/resolv.conf 
添加如下行:
nameserver 8.8.8.8
nameserver 8.8.4.4
上面用的8.8.8.8/8.8.4.4是Google Free DNS server,當然還有很多免費的DNS server)
有些安裝后自動就添加了上面的DNS配置了,不需要再添加了


6.測試一下郵件是否能夠發送成功:
發送郵件到QQ或163郵箱測試效果
echo "content" | mail -s "title" xxx@163.com
163郵箱發送人顯示的"root@xxx.com 注意:前面有個雙引號不是單純root

echo "content" | mail -s "title" xxx@foxmail.com
在foxmail郵箱里面收到的是:發件人:root <root@xxx.com>

注意:該配置只能是發送郵件,並不能接收郵件。

要用java調用shell命令發送郵件請看本人另外一篇博客:

Java執行shell腳本並返回結果兩種方法的完整代碼 - 流風,飄然的風 - 博客園
https://www.cnblogs.com/zdz8207/p/java-linux-shell.html

 

-------------
7.查看log,確認郵件發送狀態:
Postfix郵件的log位置是:/var/log/maillog
發送成功的話,會返回250和OK,也可以去自己的郵件客戶端查收。
mailq :會列出當前在postfix發送隊列中的所有郵件
postsuper -d ALL:刪除當前等待發送隊列的所有郵件,包括發送失敗的退信

查看進程:

# ps -ef | grep postfix
root 6104 1 0 01:45 ? 00:00:00 /usr/libexec/postfix/master -w
postfix 6106 6104 0 01:45 ? 00:00:00 qmgr -l -t unix -u
postfix 7946 6104 0 03:25 ? 00:00:00 pickup -l -t unix -u
root 8007 5166 0 03:27 pts/0 00:00:00 grep --color=auto postfix

 ============

CentOS中禁止掉郵件服務postfix:
查看狀態
# service postfix status
systemctl status postfix
systemctl is-enabled postfix
禁止掉郵件服務postfix:
sudo systemctl stop postfix
sudo systemctl disable postfix
在操作之前和之后,可以查看狀態:
systemctl status postfix
systemctl is-enabled postfix
================================
# systemctl is-enabled postfix
enabled
[root@alternative-unicorn-2 ~]# sudo systemctl stop postfix
[root@alternative-unicorn-2 ~]# sudo systemctl disable postfix
Removed symlink /etc/systemd/system/multi-user.target.wants/postfix.service.
[root@alternative-unicorn-2 ~]# systemctl is-enabled postfix
disabled
[root@alternative-unicorn-2 ~]# systemctl status postfix
● postfix.service - Postfix Mail Transport Agent
Loaded: loaded (/usr/lib/systemd/system/postfix.service; disabled; vendor preset: disabled)
Active: inactive (dead)


免責聲明!

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



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