阿里云CentOS Linux6.5服务器上用postfix +dovecot搭建邮件服务器 完整篇


注:本文的邮件服务器只用于发送邮件,也就是STMP服务器。

企业域名1082.cn

邮件服务器 mail.1082.cn

Mail服务器名: mail.1082.cn

IP:120.24.70.237

一、准备工作

1. 为邮件服务器添加DNS解析
虽然不加DNS解析也能把邮件发出去,但会被大多数邮件服务器当作垃圾邮件。根据我们的实际经验,需要添加三条DNS解析记录:A记录、MX记录、TXT记录。比如域名1082.cn,对应的DNS记录如下:
 
2. 准备存放邮件的硬盘空间
如果用的是阿里云入门级Linux服务器,有一块20G的数据盘未挂载,需要格式化并挂载(假设这里挂载的目录是/data),具体操作步骤见之前的博文阿里云云服务器硬盘分区及挂载。
3.  准备安装Postfix sasl2 dovecot安装
[root@mail ~]# yum install postfix cyrus-sasl* dovecot
先检测dovecot软件包是否已经安装  rpm-qa dovecot
二、配置postfix
postfix是CentOS默认安装的邮件服务器软件。以下配置示例假设要配置的域名是1082.cn,邮件服务器主机名是mail.1082.cn。
1. 打开postfix的配置文件
vi /etc/postfix/main.cf
2. :75 光标移至第75行,修改myhostname
myhostname = mail.1082.cn
3. :83 光标移至第83行,修改mydomain
mydomain = 1082.cn
4. :99 光标移至第99行,修改myorigin
myorigin = $mydomain
5. :116光标移至第116行,修改inet_interfaces
inet_interfaces = all
6. :119光标移至第119行,修改inet_protocols
inet_protocols = ipv4
7. :164光标移至第164行,添加$mydomain
mydestination = $myhostname,localhost.$mydomain, localhost, $mydomain
8. :251光标移至第251行,修改mynetworks
mynetworks_style = subnet  //指定全网IP,这两个参数设置非常重要(下面)
9. :264光标移至第264行,修改mynetworks
mynetworks = 127.0.0.0/8  0.0.0.0/0 #表示全部网络地址可以访问邮件
子网掩码(netmask)转换器:Network and IP address calculator
10. :419 光标移至第419行,设置home_mailbox
home_mailbox = Maildir/   
//如果采用系统帐号作为邮件用户名,该目录为帐号下的目录最后面添加(sasl加密算法支持)
11. :425 光标移至第425行,设置mail_spool_directory,/data是之前挂载的数据盘,mail目录需要通过mkdir命令创建
mail_spool_directory = /data/mail
12. 重启postfix使设置生效
service postfix restart
三、用telnet测试邮件服务器是否正常
1. 在邮件服务器上安装telnet
yum install telnet
2. 连接服务器SMTP端口
telnet 127.0.0.1 25
3. 填写发件人
MAIL FROM:<test@szniuwang.com>
回车
4. 填写收件人
RCPT TO:contact@szniuwang.com 
回车
5. 撰写邮件内容
5.1 开始写邮件内容
DATA                   
回车
5.2 输入标题
Subject: test message
回车
5.3 输入内容,并以.号结束
test body
.
如果显示下面的信息,说明邮件进入发送队列
250 2.0.0 Ok: queued as 88D6D32A94
四、授权其他服务器通过该邮件服务器发送邮件
未授权的情况下,如果在其他服务器telnet这台服务器,在输入收件人之后会出现Relay access denied错误
RCPT TO:contact@szniuwang.com
554 5.7.1 <contact@szniuwang.com>:Relay access denied
解决方法:
vi /etc/postfix/main.cf,:264将光标移至第264行,在mynetworks的值之后加这台的服务器IP地址,比如:
mynetworks = 127.0.0.0/8 58.33.14.124  0.0.0.0/0
0.0.0.0/0  表示全部网络 service postfix restart之后,问题解决。
五、其他问题
收到上面telnet发出的邮件时,你会发现收件人信息显示的是undisclosedrecipients,解决方法是在DATA命令之后,输入Subject之前,分别输入:
FROM:test@szniuwang.com
TO:contact@szniuwang.com
六、参考资料:
Install Postfix to configure SMTP Server
Postfix - using Telnet to test postfix
"To" field is populated with "undisclosed-recipients"sending emails from a telnet session.
 
 
 
六   dovecot 部分安装
 
1. 打开postfix的配置文件
vi /etc/postfix/main.cf
在文件最后增加以下内容
 
smtpd_sasl_auth_enable = yes
smtpd_sasl_security_options = noanonymous
broken_sasl_auth_clients = yes
smtpd_recipient_restrictions =permit_sasl_authenticated,reject_unauth_destination,permit_mynetworks
smtpd_client_restrictions =permit_sasl_authenticated
 
2. 从命令行选择可用的 MTA
 [root@mail ~]# alternatives --config mta
 
 
 
3. 修改系统帐号验证
 
 [root@mail ~]# vi /etc/sysconfig/saslauthd
SOCKETDIR=/var/run/saslauthd
MECH=shadow                //采用系统帐号验证
 
 
 
4. 修改或者添加 [root@mail ~]#vi /etc/sasl2/smtpd.conf
 
pwcheck_method: saslauthd
mech_list: plain login
log_level:3
 
5. [root@mail ~]# vi /etc/dovecot/dovecot.conf    修改或者添加 
protocols = imap pop3
listen = *
 
6. Dovecot 配置 dovecot.conf 文件配置邮件接收的存储位置,要和postfix配置目录一致
/etc/dovecot/conf.d/10-mail.conf 去掉下面一行注释
mail_location = maildir:~/Maildir
 
 
7.  重新启动程序
[root@mail ~]# /etc/init.d/postfix restart
[root@mail ~]# /etc/init.d/dovecot restart
[root@mail ~]# /etc/init.d/saslauthdrestart
 
 
8.  设置开机自动启动
[root@mail ~]#chkconfig –level 345 postfixon
[root@mail ~]#chkconfig –level 345 dovecoton
[root@mail ~]#chkconfig –level 345saslauthd on
 
 
9. 添加系统邮件帐号
(1)先新建系统帐号
[root@mail ~]# useradd  -s /sbin/nologin -m  wangsl
[root@mail ~]# passwd   test //密码 test
邮件存放目录
/home/pay/Maildir
 
(2)添加邮箱账号
[root@mail ~]# /usr/sbin/testsaslauthd  -u test  -p test
 
上面这步所使用的用户名和密码,系统中必须有该用户和对应的密码,否则会报错
返回:ok,successd,执行成功。
 
10. 系统本机测试 110 端口邮件接收情况
 
[root@smtp root]# telnet localhost 110
Trying 127.0.0.1...
Connected to smtp.test.com (127.0.0.1).
Escape character is '^]'.
+OK Dovecot ready.
user test
+OK
pass test
+OK Logged in.
list
+OK 1 messages:
retr 1
dele 1
1 481
 
下面命令可以测试发送邮件
mail -s 'Postfix mail test'wangsl@yeah.net< /etc/hosts
有时候出现邮件目录错误,是配置邮件接收目录不正确
[root@rhel6 ~]# su - oracle
[oracle@rhel6 ~]$ mkdir -p/u01/oracle/mail/.imap/INBOX
[oracle@rhel6 ~]$ exit
 
 
11.  foxmail 测试
添加账号:pop3 stmp 都需要设置为域名地址(mail.szniuwang.com)
最后就可以通过outlook,foxmail等客户端测试邮件服务器了。
 
 
Foxmail 深圳IMAP接收方式
 
Foxmail 设置pop3 邮件接收方式
Outlook 2007 客户端设置
 
完全正常后删除测试邮箱,以免存在系统漏洞。
#userdel –r test
userdel –f test
 
postconf –d message_size_limit 显示默认邮件大小
 
出现发送邮件 Relay access denied 错误需要 outlook设置正常才可以的 
 'wangsl@yeah.net',时间为 2014/10/27 11:41
            554 5.7.1 <wangsl@yeah.net>:Relay access denied
 
 
 
 
 
修改邮件附件发送大小限制
/etc/postfix/main.cf
message_size_limit = 20480000
 
 
 
 
 
 
 
 
 
 
 
 
 
常见问题1     testsaslauthd出现0: NO "authentication failed"错误提示的解决办法
 
在停止sendmail服务: service sendmail stop  或者:   /etc/rc.d/init.d/sendmail stop  后卸载掉sendmail:  rpm -e sendmail
后安装了postfix    yum -y installpostfix。
 
配置好main.cf
 
安装 cyrus-sasl 软件包: yum -y install cyrus-sasl*
 
安装SMTP认证组件:yum install -y dovecot saslauthd*
 
运行saslauth 
saslauthd -v
#如果显示类似authenticationmechanisms:getpwent pam shadow则可显示saslauthd支持的认证机制
 
编辑/etc/sysconfig/saslauthd文件,
vim /etc/sysconfig/saslauthd
#确认其为MECH=pam
启动saslauthd    service saslauthdstart
 
 
运行:
/usr/sbin/testsaslauthd –u user –p‘password’
 
这时总是出错:0: NO "authentication failed"
 
该怎么办呢?
 
其实很简单:vi /etc/sysconfig/saslauthd
 
#MECH=pam
 
改成:
 
MECH=shadow
 
FLAGS=
 
然后重启saslauthd: service saslauthd restart
 
再来测试 /usr/sbin/testsaslauthd –u myuserid –p ‘mypassword’  //这里的账号和密码要换成你的linux 的用户名和密码
 
0: OK "Success."
 
终于成功了。
 
软件包下载
#yumdownloader samba
 
 完整篇 
 
注:本文的邮件服务器只用于发送邮件,也就是STMP服务器。
企业域名1082.cn
邮件服务器 mail.1082.cn
Mail服务器名: mail.1082.cn
IP:120.24.70.237
一、准备工作
1. 为邮件服务器添加DNS解析
虽然不加DNS解析也能把邮件发出去,但会被大多数邮件服务器当作垃圾邮件。根据我们的实际经验,需要添加三条DNS解析记录:A记录、MX记录、TXT记录。比如域名1082.cn,对应的DNS记录如下:
 
2. 准备存放邮件的硬盘空间
如果用的是阿里云入门级Linux服务器,有一块20G的数据盘未挂载,需要格式化并挂载(假设这里挂载的目录是/data),具体操作步骤见之前的博文阿里云云服务器硬盘分区及挂载。
3.  准备安装Postfix sasl2 dovecot安装
[root@mail ~]# yum install postfix cyrus-sasl* dovecot
先检测dovecot软件包是否已经安装  rpm-qa dovecot
二、配置postfix
postfix是CentOS默认安装的邮件服务器软件。以下配置示例假设要配置的域名是1082.cn,邮件服务器主机名是mail.1082.cn。
1. 打开postfix的配置文件
vi /etc/postfix/main.cf
2. :75 光标移至第75行,修改myhostname
myhostname = mail.1082.cn
3. :83 光标移至第83行,修改mydomain
mydomain = 1082.cn
4. :99 光标移至第99行,修改myorigin
myorigin = $mydomain
5. :116光标移至第116行,修改inet_interfaces
inet_interfaces = all
6. :119光标移至第119行,修改inet_protocols
inet_protocols = ipv4
7. :164光标移至第164行,添加$mydomain
mydestination = $myhostname,localhost.$mydomain, localhost, $mydomain
8. :251光标移至第251行,修改mynetworks
mynetworks_style = subnet  //指定全网IP,这两个参数设置非常重要(下面)
9. :264光标移至第264行,修改mynetworks
mynetworks = 127.0.0.0/8  0.0.0.0/0 #表示全部网络地址可以访问邮件
子网掩码(netmask)转换器:Network and IP address calculator
10. :419 光标移至第419行,设置home_mailbox
home_mailbox = Maildir/   
//如果采用系统帐号作为邮件用户名,该目录为帐号下的目录最后面添加(sasl加密算法支持)
11. :425 光标移至第425行,设置mail_spool_directory,/data是之前挂载的数据盘,mail目录需要通过mkdir命令创建
mail_spool_directory = /data/mail
12. 重启postfix使设置生效
service postfix restart
三、用telnet测试邮件服务器是否正常
1. 在邮件服务器上安装telnet
yum install telnet
2. 连接服务器SMTP端口
telnet 127.0.0.1 25
3. 填写发件人
MAIL FROM:<test@szniuwang.com>
回车
4. 填写收件人
RCPT TO:contact@szniuwang.com 
回车
5. 撰写邮件内容
5.1 开始写邮件内容
DATA                   
回车
5.2 输入标题
Subject: test message
回车
5.3 输入内容,并以.号结束
test body
.
如果显示下面的信息,说明邮件进入发送队列
250 2.0.0 Ok: queued as 88D6D32A94
四、授权其他服务器通过该邮件服务器发送邮件
未授权的情况下,如果在其他服务器telnet这台服务器,在输入收件人之后会出现Relay access denied错误
RCPT TO:contact@szniuwang.com
554 5.7.1 <contact@szniuwang.com>:Relay access denied
解决方法:
vi /etc/postfix/main.cf,:264将光标移至第264行,在mynetworks的值之后加这台的服务器IP地址,比如:
mynetworks = 127.0.0.0/8 58.33.14.124  0.0.0.0/0
0.0.0.0/0  表示全部网络 service postfix restart之后,问题解决。
五、其他问题
收到上面telnet发出的邮件时,你会发现收件人信息显示的是undisclosedrecipients,解决方法是在DATA命令之后,输入Subject之前,分别输入:
FROM:test@szniuwang.com
TO:contact@szniuwang.com
六、参考资料:
Install Postfix to configure SMTP Server
Postfix - using Telnet to test postfix
"To" field is populated with "undisclosed-recipients"sending emails from a telnet session.
 
 
 
六   dovecot 部分安装
 
1. 打开postfix的配置文件
vi /etc/postfix/main.cf
在文件最后增加以下内容
 
smtpd_sasl_auth_enable = yes
smtpd_sasl_security_options = noanonymous
broken_sasl_auth_clients = yes
smtpd_recipient_restrictions =permit_sasl_authenticated,reject_unauth_destination,permit_mynetworks
smtpd_client_restrictions =permit_sasl_authenticated
 
2. 从命令行选择可用的 MTA
 [root@mail ~]# alternatives --config mta
 
 
 
3. 修改系统帐号验证
 
 [root@mail ~]# vi /etc/sysconfig/saslauthd
SOCKETDIR=/var/run/saslauthd
MECH=shadow                //采用系统帐号验证
 
 
 
4. 修改或者添加 [root@mail ~]#vi /etc/sasl2/smtpd.conf
 
pwcheck_method: saslauthd
mech_list: plain login
log_level:3
 
5. [root@mail ~]# vi /etc/dovecot/dovecot.conf    修改或者添加 
protocols = imap pop3
listen = *
 
6. Dovecot 配置 dovecot.conf 文件配置邮件接收的存储位置,要和postfix配置目录一致
/etc/dovecot/conf.d/10-mail.conf 去掉下面一行注释
mail_location = maildir:~/Maildir
 
 
7.  重新启动程序
[root@mail ~]# /etc/init.d/postfix restart
[root@mail ~]# /etc/init.d/dovecot restart
[root@mail ~]# /etc/init.d/saslauthdrestart
 
 
8.  设置开机自动启动
[root@mail ~]#chkconfig –level 345 postfixon
[root@mail ~]#chkconfig –level 345 dovecoton
[root@mail ~]#chkconfig –level 345saslauthd on
 
 
9. 添加系统邮件帐号
(1)先新建系统帐号
[root@mail ~]# useradd  -s /sbin/nologin -m  wangsl
[root@mail ~]# passwd   test //密码 test
邮件存放目录
/home/pay/Maildir
 
(2)添加邮箱账号
[root@mail ~]# /usr/sbin/testsaslauthd  -u test  -p test
 
上面这步所使用的用户名和密码,系统中必须有该用户和对应的密码,否则会报错
返回:ok,successd,执行成功。
 
10. 系统本机测试 110 端口邮件接收情况
 
[root@smtp root]# telnet localhost 110
Trying 127.0.0.1...
Connected to smtp.test.com (127.0.0.1).
Escape character is '^]'.
+OK Dovecot ready.
user test
+OK
pass test
+OK Logged in.
list
+OK 1 messages:
retr 1
dele 1
1 481
 
下面命令可以测试发送邮件
mail -s 'Postfix mail test'wangsl@yeah.net< /etc/hosts
有时候出现邮件目录错误,是配置邮件接收目录不正确
[root@rhel6 ~]# su - oracle
[oracle@rhel6 ~]$ mkdir -p/u01/oracle/mail/.imap/INBOX
[oracle@rhel6 ~]$ exit
 
 
11.  foxmail 测试
添加账号:pop3 stmp 都需要设置为域名地址(mail.szniuwang.com)
最后就可以通过outlook,foxmail等客户端测试邮件服务器了。
 
 
Foxmail 深圳IMAP接收方式
 
Foxmail 设置pop3 邮件接收方式
Outlook 2007 客户端设置
 
完全正常后删除测试邮箱,以免存在系统漏洞。
#userdel –r test
userdel –f test
 
postconf –d message_size_limit 显示默认邮件大小
 
出现发送邮件 Relay access denied 错误需要 outlook设置正常才可以的 
 'wangsl@yeah.net',时间为 2014/10/27 11:41
            554 5.7.1 <wangsl@yeah.net>:Relay access denied
 
 
 
 
 
修改邮件附件发送大小限制
/etc/postfix/main.cf
message_size_limit = 20480000
 
 
 
 
 
 
 
 
 
 
 
 
 
常见问题1     testsaslauthd出现0: NO "authentication failed"错误提示的解决办法
 
在停止sendmail服务: service sendmail stop  或者:   /etc/rc.d/init.d/sendmail stop  后卸载掉sendmail:  rpm -e sendmail
后安装了postfix    yum -y installpostfix。
 
配置好main.cf
 
安装 cyrus-sasl 软件包: yum -y install cyrus-sasl*
 
安装SMTP认证组件:yum install -y dovecot saslauthd*
 
运行saslauth 
saslauthd -v
#如果显示类似authenticationmechanisms:getpwent pam shadow则可显示saslauthd支持的认证机制
 
编辑/etc/sysconfig/saslauthd文件,
vim /etc/sysconfig/saslauthd
#确认其为MECH=pam
启动saslauthd    service saslauthdstart
 
 
运行:
/usr/sbin/testsaslauthd –u user –p‘password’
 
这时总是出错:0: NO "authentication failed"
 
该怎么办呢?
 
其实很简单:vi /etc/sysconfig/saslauthd
 
#MECH=pam
 
改成:
 
MECH=shadow
 
FLAGS=
 
然后重启saslauthd: service saslauthd restart
 
再来测试 /usr/sbin/testsaslauthd –u myuserid –p ‘mypassword’  //这里的账号和密码要换成你的linux 的用户名和密码
 
0: OK "Success."
 
终于成功了。
 
软件包下载
#yumdownloader samba