通过阿里云服务发送阿里云企业邮件

原文链接:https://dy.163.com/article/DV56MMN00525C3HA.html?referFrom=blog.csdn.net

第一步:准备好相关便于发邮件的邮件服务商。不建议使用个人QQ,一是发多了,容易被封!我们用于Fail2ban 的报警的,哪一天,多个攻击,一小时发几十封都可能是正常的!

  因此我选择了阿里云企业版免费邮箱,一两个小时发个几百封是没有问题!其邮箱配置如下:

  协议 服务器地址 服务器端口号(常规) 服务器端口号(加密) SMTP smtp.qiye.aliyun.com 25 465

  第二步:把Linux服务器的安全组、IPtables 、控制面板的端口、如果有安全狗等第三方安全软件,把25和465的端口的进出入端口全部进行开放!

  IPtables的使用如下命令:

  iptables -A INPUT -p tcp --dport 25 -j ACCEPT
iptables -A OUTPUT -p tcp --sport 25 -j ACCEPT

  iptables -A INPUT -p tcp --dport 465 -j ACCEPT
iptables -A OUTPUT -p tcp --sport 465 -j ACCEPT

  第三步:记得重启第二步相关的软件,如iptables 因为有些规则,需要重启才能生效! iptable 重启可使用这个命令:systemctl reload iptable.service

  第四步:用Notepad 或记事本或代码编辑软件,根据邮件及密码,编好类似如下的命令,等会其它步骤需要用到:

  set from=用户名@wsem.net.cn
set smtp=smtps://smtp.qiye.aliyun.com:465
set smtp-auth-user=用户名@wsem.net.cn
set smtp-auth-password=***************
set smpt-auth=login

  set smtp-use-starttls
set ssl-verify=ignore
set nss-config-dir=/root/.certs

  第五步:把Linux 的其它邮件服务器,先停止,不然可以安装Mailx可以出现意外!不管自己有没有安装,常见的如下:

  service sendmail stop

  chkconfig sendmail off

  service postfix stop

  chkconfig postfix off

  第六步:使用Yum命令开启安装Mailx

  yum -y install mailx

  第七步:配置 Mail的发邮件参数,就是需要用到前面的第四步。把第四步的内容直接补进下面这个文件中,位置也以前,也可中间或结尾。

  vi /etc/mail.rc

  按i健,把第四步的复制进去后,按ESC健,再按:wq 按回车健

  第八步:可省略,有则更好;但很多人容易出错。配置SSL来发邮件。

  先生成证书:

  echo -n | openssl s_client -connect smtp.qiye.aliyun.com:465 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > ~/.certs/qiye.aliyun.com.crt

  certutil -A -n "GeoTrust SSL CA" -t "C,," -d ~/.certs -i ~/.certs/qiye.aliyun.com.crt

  certutil -A -n "GeoTrust Global CA" -t "C,," -d ~/.certs -i ~/.certs/qiye.aliyun.com.crt

  截止上述步骤,如果使用root 用户的,在 root/.certs 多出 qiye.aliyun.com.crt 这个证书。备注.certs 是设置了隐藏模式,是为了安全。

  接下来的这一步非常关键:打开证书的存放目录,查看证书的是否可以读写?最好是RW-R-R权限

  [root@host~]# cd /root/.certs
[root@host .certs]# ll
total 84
-rw------- 1 root root 65536 Oct 27 13:04 cert8.db
-rw------- 1 root root 16384 Oct 27 13:04 key3.db
-rw-r--r-- 1 root root 2610 Oct 27 13:03 qiye.aliyun.com.crt
-rw-r--r-- 1 root root 2610 Oct 27 12:56 qq.crt
-rw------- 1 root root 16384 Oct 27 11:27 secmod.db

  然后,输入命令:

  [root@host .certs]# certutil -A -n "GeoTrust SSL CA - G3" -t "Pu,Pu,Pu" -d ./ -i qiye.aliyun.com.crt
Notice: Trust flag u is set automatically if the private key is present.

  有这个提示,说明SSL证书配置生成及安装完成 。

  第九步:进行发邮件测试,有很多的方法,建议先使用这种方法进行测试:张开辉推荐两种

  echo hello word | mailx -v -s "demo title" abc@139.com

  把abc@139.com改为需要测试的收件的邮件地址。如果收到信,说明测试成功!

  正常这样发信:

  echo "have you received" | mailx -s "Hack Reports" abc@139.com

  以下是张开辉在配置Mailx 中所遇到的问题集合,分享出来,供大家参考!

  问1:没有任何错误提示,如果提示:Error in certificate: Peer’s certificate issuer has been marked as not trusted by the.

  答:说明SSL安装及配置出了问题。

  问2:如果配置过程出现:unable to open “qiye.aliyun.com.crt” for reading (-5950, 2).

  答:正常是“certutil -A -n "GeoTrust SSL CA - G3" -t "Pu,Pu,Pu" -d ./ -i qiye.aliyun.com.crt”时,没有事先进入到certs文件目录下。或者crt文件不可读!

  问3:所有都正常安装了,收件箱还是没有收到邮件?

  答:可能服务器或防火墙或IPtable或安全端口设置,没有开启25或465端口。

  问4:是否一定需要SSL证书,或SSL证书一定要配置成功?

  答:不一定的,即使SSL没有配置成功,我们在配置时进行ignore 设置:set ssl-verify=ignore

  问5:QQ邮箱,密码配置了,也无法成功!怎么配置?

  答:现在QQ邮箱使用授权码,才能发邮件,因此仅输入授权码即可!配置参考如下:

  set smtp=smtps://smtp.qq.com:465
set smtp-auth-user=375923486@qq.com
set smtp-auth-password= 输入授权码
set smtp-auth=login
set ssl-verify=ignore
set nss-config-dir=/root/.certs

  问6:163配置后,出现554 DT:SPM 163 smtp3

  答:554 DT:SPM 发送的邮件内容包含了未被许可的信息,或被系统识别为垃圾邮件。请检查是否有用户发送病毒或者垃圾邮件。可更换相关的别的发信邮件服务器。

  问7:不知道生成的证书放在哪里,怎么办?

  答:输入以下命令可以查找证书存放在哪里:whereis .certs

  问8:所有配置都正确了,为什么还是不行!提示:

  SSL/TLS handshake failed: SSL received a malformed Alert record.
"/root/dead.letter" 11/306
. . . message not sent.

  答:请在mail.rc 中,将第四步中的命令set smtp-use-starttls去掉

email邮箱服务连接配置:

Properties prop = new Properties();
prop.setProperty("mail.host",“smtp.mxhichina.com”);

prop.setProperty("mail.transport.protocol", "smtp");
prop.setProperty("mail.smtp.auth", "true");
//邮箱发送服务器端口,这里设置为465端口
prop.setProperty("mail.smtp.port", "465");
prop.setProperty("mail.smtp.socketFactory.port", "465");
//开启ssl加密,不添加此配置无法连接465端口
prop.setProperty("mail.smtp.ssl.enable", "true");
原文地址:https://www.cnblogs.com/yunian139/p/13534910.html