邮件发送总是提示连接不到服务器
问题原因是因为开始没有配置ssl相关协议,后面是因为设置的实效时间太短,导致超时报错
<?xml version="1.0" encoding="GBK"?>
<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"
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">
<context:annotation-config />
<!-- 1.指定引入其他资源文件.properties文件 -->
<!-- <context:property-placeholder location="classpath:config/jdbc.properties"/>-->
<!-- 2.配置简单邮件消息对象 -->
<bean id="mailMessage" class="org.springframework.mail.SimpleMailMessage">
<!-- 此时我们只需要注入发件箱名称即可。不要注入主题,正文,收件箱等不固定的信息 -->
<property name="from" value=""></property>
</bean>
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<!-- <property name="host" value=""></property>-->
<property name="host" value="smtp.chinatelecom.cn"></property>
<property name="username" value=""></property>
<property name="password" value=""></property>
<property name="defaultEncoding" value="UTF-8"></property>
<property name="protocol" value="smtp"></property>
<property name="port" value="465"></property>
<property name="javaMailProperties">
<props>
<!-- 是否需要验证 -->
<prop key="mail.smtp.auth">true</prop>
<!-- 是否需要debug的信息 -->
<prop key="mail.smtp.debug">true</prop>
<prop key="mail.smtp.socketFactory.port">465</prop>
<prop key="mail.default-encoding">UTF-8</prop>
<prop key="mail.smtp.ssl.enable">true</prop>
<prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
<!-- 设置发送超时时间,以秒为单位。0为永不超时 -->
<prop key="mail.smtp.timeout">4000</prop>
<prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
</props>
</property>
</bean>
</beans>