搭建邮件服务器 Postfix + Dovecot (CentOS)

时间:2024-04-14 22:06:12

最近分配到一台ECS服务器,需要搭建一台邮件服务器。

查了一波资料选择了Postfix (smtp)和 Dovecot(pop3).

推荐教程:http://www.cnblogs.com/zlbeidou/p/3436332.html (我的是CentOS6.5 如果是Centos7.*的注意selinux,防止出现坑,ubuntu用此教程的话注意使用sudo指令,其他基本相同)

1:配置 DNS 服务器并添加邮件服务器 MX 记录. 并让 ISP 支持你的静态 IP 与域名. (局域网搭建跳过此步骤,服务器必须)

2:安装 Postfix

[root@server ~]# yum install postfix -y

配置 Postfix

打开 /etc/postfix/main.cf 更改如下配置:

## Line no 75 - Uncomment and set your mail server FQDN ##
myhostname = server.cnblogs.com 你的主机名称(比我是mail.xxx.com,需要加到MX记录,优先级10)

## Line 83 - Uncomment and Set domain name ##
mydomain = cnblogs.com 你的域名(比如我是xxx.com)

## Line 99 - Uncomment ##
myorigin = $mydomain

## Line 116 - Set ipv4 ##
inet_interfaces = all

## Line 119 - Change to all ##
inet_protocols = all

## Line 164 - Comment ##

#mydestination = $myhostname, localhost.$mydomain, localhost,

## Line 165 - Uncomment ##
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain

## Line 264 - Uncomment and add IP range ##
mynetworks = 192.168.1.0/24, 127.0.0.0/8(也可以不改)

## Line 419 - Uncomment ##
home_mailbox = Maildir/ (邮件存放目录)

保存并退出, 重启 Postfix 服务:

[root@server ~]# /etc/init.d/postfix start
[root@server ~]# /etc/init.d/postfix status
master (pid  1290) is running...
[root@server ~]# chkconfig postfix on

测试 Postfix

通过 telnet 进行测试, 没有 telnet 可通过 yum 安装, 一下出现红色字体表示输入.

新建测试用户 fourone:

[root@server ~]# useradd fourone
[root@server ~]# passwd fourone

测试功能

[root@server ~]# telnet localhost smtp
Trying ::1...
Connected to localhost.
Escape character is \'^]\'.
220 server.cnblogs.com ESMTP Postfix
ehlo localhost
250-server.cnblogs.com
250-PIPELINING
250-SIZE 10240000
250-VRFY
250-ETRN
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN
mail from:<fourone>
250 2.1.0 Ok
rcpt to:<fourone>
250 2.1.5 Ok
data
354 End data with <CR><LF>.<CR><LF>
hello fourone
.
250 2.0.0 Ok: queued as 3E68E284C
quit
221 2.0.0 Bye
Connection closed by foreign host.

进入 fourone mail 目录检查邮件:

[root@server ~]# ls /home/fourone/Maildir/new/
1369816264.Vfd00I41ec0M251771.server.cnblogs.com
[root@server ~]# cat /home/fourone/Maildir/new/1369816264.Vfd00I41ec0M251771.server.cnblogs.com 
Return-Path: <fourone@cnblogs.com>
X-Original-To: fourone
Delivered-To: fourone@cnblogs.com
Received: from localhost (localhost [IPv6:::1])
    by server.cnblogs.com (Postfix) with ESMTP id 3E68E284C
    for <zl>; Wed, 29 May 2013 14:00:36 +0530 (IST)
Message-Id: <20130529083047.3E68E284C@server.cnblogs.com>
Date: Wed, 29 May 2013 14:00:36 +0530 (IST)
From: fourone@cnblogs.com
To: undisclosed-recipients:;
hello fourone
 

3:安装 Dovecot

Dovecote 是一款开源 IMAP/POP3 邮件服务器 for Unix/Linux systems.

[root@server ~]# yum install dovecot

配置 Dovecot

打开 /etc/dovecot/dovecot.conf 更改如下配置:

## Line 20 - umcomment ##
protocols = imap pop3 lmtp

打开 /etc/dovecot/conf.d/10-mail.conf:

## Line 24 - uncomment ##
mail_location = maildir:~/Maildir (遇上面postfix一致)

打开 /etc/dovecot/conf.d/10-auth.conf:

## line 9 - uncomment##
disable_plaintext_auth = yes (出现如果不要ssl验证的则是no)

## Line 99- Add a letter "login" ##
auth_mechanisms = plain login

打开 /etc/dovecot/conf.d/10-master.conf:

## Line 83, 84 - Uncomment and add "postfix"
   #mode = 0600
   user = postfix
   group = postfix

开启 Dovecot 服务:

[root@server ~]# service dovecot start
Starting Dovecot Imap:                                     [  OK  ]
[root@server ~]# chkconfig dovecot on (加入系统启动项)

测试 Dovecot

[root@server ~]# telnet localhost pop3
Trying ::1...
Connected to localhost.
Escape character is \'^]\'.
+OK Dovecot ready.
user fourone
+OK
pass 111111      ## Here 111111 is password of user "fourone" ##
+OK Logged in.
list
+OK 1 messages:
1 427
.
retr 1
+OK 427 octets
Return-Path: <fourone@cnblogs.com>
X-Original-To: fourone
Delivered-To: fourone@cnblogs.com
Received: from localhost (localhost [IPv6:::1])
    by server.cnblogs.com (Postfix) with ESMTP id 3E68E284C
    for <zl>; Wed, 29 May 2013 14:00:36 +0530 (IST)
Message-Id: <20130529083047.3E68E284C@server.cnblogs.com>
Date: Wed, 29 May 2013 14:00:36 +0530 (IST)
From: fourone@cnblogs.com
To: undisclosed-recipients:;

hello fourone
.
quit
+OK Logging out.
Connection closed by foreign host.

Dovecot 测试成功.