Spring/SpringBoot解决在阿里云上无法发送邮件问题(25端口改SSL465端口)

2021-08-04 742点热度 0人点赞 0条评论

问题描述

最近迁移一个Spring项目到阿里云,之前有一个功能smtp发邮件是正常的,到了阿里云就不能发送了(超时),到服务器的命令行上去telnet smtp服务器的25端口(telnet smtp.example.com 25),发现是超时不通的,应该是被阿里云封锁了。

问题解决

现在一般smtp服务器都配置了SSL功能(默认465端口),尝试telnet后,发现是465端口是可以的,所以需要修改发邮件程序

  • SpringBoot 下配置文件修改
# 使用 smtp 协议
spring.mail.protocol = smtp
spring.mail.host = smtp.example.com
spring.mail.port = 465
spring.mail.username = [email protected]
spring.mail.password = password
spring.mail.default-encoding = UTF-8
spring.mail.test-connection = false
spring.mail.properties.mail.smtp.auth = true
spring.mail.properties.mail.debug = false
spring.mail.properties.mail.smtp.socketFactory.class = javax.net.ssl.SSLSocketFactory
spring.mail.properties.mail.smtp.socketFactory.port = 465
spring.mail.properties.mail.smtp.starttls.enable = true

关于Spring下如何配置发送邮件,请查看我之前的文章:https://blog.terrynow.com/2021/07/26/java-springboot-send-mail-example-include-html-inline-attachment/

  • SpringMVC 下配置文件修改
<!-- 发送邮件配置 -->
<bean id="javaMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
    <property name="host" value="${mail.host}"/>
    <property name="javaMailProperties">
        <props>
            <prop key="mail.smtp.auth">true</prop>
            <prop key="mail.smtp.timeout">25000</prop>
            <prop key="mail.smtp.starttls.enable">true</prop>
            <prop key="mail.smtp.socketFactory.port">465</prop>
            <prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
        </props>
    </property>
    <property name="username" value="${mail.username}"/>
    <property name="password" value="${mail.password}"/>
    <property name="port" value="465"/>
    <property name="defaultEncoding" value="UTF-8"/>
</bean>

关于SpringMVC下如何配置发送邮件,请查看我之前写的文章:https://blog.terrynow.com/2021/07/24/java-spring-mvc-send-mail-example-include-html-inline-attachment/

admin

这个人很懒,什么都没留下

文章评论

您需要 登录 之后才可以评论