ECS阿里云服务器默认禁了smtp端口(25)因此 无法发送邮件
之前写的java项目部署到阿里云上时,在进行邮件服务时报错了,出现超时错误,当时我就纳闷,在我的windows上面部署时是没有出现这样的问题的。然后考虑了一下是不是防火墙的原因,就去查看了防火墙策略,并没有把25给禁了。
又想到可能是服务器的安全组规则把端口25给墙了,然后就去阿里云的控制台把端口25的出入都给开了,还是没用。
我先试了一下telnet,果然是会超时,那说明不是这两个的原因。
再上网查了一下原因,果然是:
阿里云ecs默认把smtp端口25给屏蔽了,需要开启的话要上控制台去申请,不能直接开启。
去申请的话有点麻烦,我就想用别的端口来替代,就上网看到博客用ssl来替代。
我是用ssm完成的项目,修改spring的配置文件和mail.properties中的端口号为ssl的465端口就可以了
我采用的是xml文件配置:
applicationContext-mail.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:property-placeholder location="classpath:config/mail.properties" ignore-unresolvable="true"/>
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="${mail.host}"></property>
<property name="port" value="${mail.port}"></property>
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.timeout">5000</prop>
<prop key="mail.smtp.ssl.enable">true</prop>
<prop key="mail.smtp.socketFactory.port">${mail.port}</prop>
<prop key="mail.smtp.socketFactory.class">${mail.SocketFactory}</prop>
</props>
</property>
<property name="username" value="${mail.username}"></property>
<property name="password" value="${mail.password}"></property>
</bean>
</beans>
mail.properties
mail.host=smtp.163.com
mail.port=465
mail.username=用户名
mail.password=密码
mail.SocketFactory=javax.net.ssl.SSLSocketFactory
修改完大功告成!不过确实阿里云这个有点坑,应该有一个屏蔽端口的通告,我们才不会踩到这么多坑。。