搭建服务环境: Centos 6
配置域名hosts: mail.demonC6.com
1.清理系统自带的邮件软件
# rpm -qa | grep sendmail* # rpm -e sendmail* --nodeps
2. 安装postfix 和 dovecot 软件
postfix 是发送邮件服务器,可以将邮件发送到该服务器上, 监听端口为 25(SMTP)
dovecot 是接受邮件服务器, 可以到服务器上取回自己的邮件, 监听端口是 110(pop3)
yum -y install postfix*
3. 配置postfix
vim /etc/postfix/main.cf
需要修改的配置如下几项,红色表示需要开启), 蓝色表示需要注释关闭:
1) 更改邮件主机名和域名
75 myhostname = mail.demonC6.com 76 #myhostname = virtual.domain.tld 77 78 # The mydomain parameter specifies the local internet domain name. 79 # The default is to use $myhostname minus the first component. 80 # $mydomain is used as a default value for many other configuration 81 # parameters. 82 # 83 mydomain = demonC6.com
2) 修改组织
94 # For the sake of consistency between sender and recipient addresses, 95 # myorigin also specifies the default domain name that is appended 96 # to recipient addresses that have no @domain part. 97 # 98 myorigin = $myhostname 99 myorigin = $mydomain
3) 修改监听的地址
113 inet_interfaces = all 114 #inet_interfaces = $myhostname 115 #inet_interfaces = $myhostname, localhost 116 #inet_interfaces = localhost
4) 修改收件人
164 mydestination = $myhostname,$mydomain 165 #mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain 166 #mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain, 167 # mail.$mydomain, www.$mydomain, ftp.$mydomain
5) 修改网卡所在的网段
264 mynetworks = 192.168.10.0/24, 127.0.0.0/8 265 #mynetworks = $config_directory/mynetworks 266 #mynetworks = hash:/etc/postfix/network_table
6) 修改转发的邮件域名
296 relay_domains = $mydestination
7) 修改postfix 邮件别名 (下面两项默认是开启的,不用动)
385 #alias_maps = dbm:/etc/aliases 386 alias_maps = hash:/etc/aliases 387 #alias_maps = hash:/etc/aliases, nis:mail.aliases 388 #alias_maps = netinfo:/aliases 389 390 # The alias_database parameter specifies the alias database(s) that 391 # are built with "newaliases" or "sendmail -bi". This is a separate 392 # configuration parameter, because alias_maps (see above) may specify 393 # tables that are not necessarily all under control by Postfix. 394 # 395 #alias_database = dbm:/etc/aliases 396 #alias_database = dbm:/etc/mail/aliases 397 alias_database = hash:/etc/aliases 398 #alias_database = hash:/etc/aliases, hash:/opt/majordomo/aliases
4 启动postfix服务,查看监听端口
# service postfix start
# netstat -anptu | grep :22 tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 2276/sshd tcp 0 40 192.168.10.129:22 192.168.10.1:56933 ESTABLISHED 6350/sshd tcp 0 0 :::22 :::* LISTEN 2276/sshd
5 自我发邮件测试
[root@demon html]# echo hello man | mail root@demonC6.com [root@demon html]# mail Heirloom Mail version 12.4 7/29/08. Type ? for help. "/var/spool/mail/root": 1 message 1 new >N 1 root Sat May 12 11:37 17/543 & 1 Message 1: From root@demonC6.com Sat May 12 11:37:49 2018 Return-Path: <root@demonC6.com> X-Original-To: root@demonC6.com Delivered-To: root@demonC6.com Date: Sat, 12 May 2018 11:37:49 +0800 To: root@demonC6.com User-Agent: Heirloom mailx 12.4 7/29/08 Content-Type: text/plain; charset=us-ascii From: root@demonC6.com (root) Status: R hello man & d & q
也可以通过telnet发邮件
[root@demon html]# telnet mail.demonC6.com 25 Trying 192.168.10.129... Connected to mail.demonC6.com. Escape character is '^]'. 220 mail.demonC6.com ESMTP Postfix mail from: root@demonC6.com 250 2.1.0 Ok rcpt to: root@demonC6.com 250 2.1.5 Ok data 354 End data with <CR><LF>.<CR><LF> Hello man, This is a test mail . 250 2.0.0 Ok: queued as EAECD44917 quit 221 2.0.0 Bye Connection closed by foreign host. You have new mail in /var/spool/mail/root [root@demon html]# mail Heirloom Mail version 12.4 7/29/08. Type ? for help. "/var/spool/mail/root": 2 messages 2 new >N 1 root@demonC6.com Sat May 12 11:42 14/513 N 2 root@demonC6.com Sat May 12 11:43 14/515 & 2 Message 2: From root@demonC6.com Sat May 12 11:43:31 2018 Return-Path: <root@demonC6.com> X-Original-To: root@demonC6.com Delivered-To: root@demonC6.com Date: Sat, 12 May 2018 11:43:10 +0800 (CST) From: root@demonC6.com To: undisclosed-recipients:; Status: R Hello man, This is a test mail & q Held 2 messages in /var/spool/mail/root
6 配置 dovecot 并启动服务
# vim /etc/dovecot/dovecot.conf
将下面这一行注释去掉,开启即可
20 protocols = imap pop3 lmtp
启动pop3服务
# service dovecot start
# netstat -anptu | grep 110 tcp 0 0 0.0.0.0:110 0.0.0.0:* LISTEN 19822/dovecot tcp 0 0 :::110 :::* LISTEN 19822/dovecot
创建两个测试用户,测试相互收发邮件
# useradd user1 && echo 123456 | passwd --stdin user1 # useradd user2 && echo 123456 | passwd --stdin user2
user1 给 user2 发送邮件
# su - user1
$ echo "user1 -> user2" | mail user2@demonC6.com
$ su user2
$ mail -u user2
Heirloom Mail version 12.4 7/29/08. Type ? for help.
"/var/mail/user2": 1 message 1 new
>N 1 user1@demonC6.com Sat May 12 12:11 17/550
& 1
Message 1:
From user1@demonC6.com Sat May 12 12:11:58 2018
Return-Path: <user1@demonC6.com>
X-Original-To: user2@demonC6.com
Delivered-To: user2@demonC6.com
Date: Sat, 12 May 2018 12:11:58 +0800
To: user2@demonC6.com
User-Agent: Heirloom mailx 12.4 7/29/08
Content-Type: text/plain; charset=us-ascii
From: user1@demonC6.com
Status: R
user1 -> user2
& q