首页 > 解决方案 > 无法通过 SMTP、Java 向海外发送电子邮件

问题描述

我很困惑,陷入了我的项目。

我想通过 SMTP JAVA 向海外电子邮件地址发送电子邮件,但收到超时消息。

我不知道为什么。

我的配置xml:

    <beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host" value="smtp.gmail.com" />
        <property name="port" value="25" />
        <property name="username" value="testing@gmail.com" />
        <property name="password" value="password" />

        <property name="javaMailProperties">
            <props>
                <prop key="mail.smtp.auth">true</prop>
                <prop key="mail.smtp.starttls.enable">true</prop>
            </props>
        </property>

    </bean>

    <bean id="emailService" class="com.dwidasa.iacssweb.service.feature.EmailService">
        <property name="mailSender" ref="mailSender" />
        <property name="simpleMailMessage" ref="customeMailMessage" />
    </bean>

    <bean id="customeMailMessage"
        class="org.springframework.mail.SimpleMailMessage">

        <property name="subject" value="RESET PASSWORD" />
        <property name="text">
        <value>
            <![CDATA[
Dear %s,
%s
            ]]>
        </value>
    </property>
    </bean>


</beans>

请帮助我,没有错误,只是连接超时。我很困惑,因为当我在本地服务器上测试时,它成功了。

错误:

造成的:

org.springframework.mail.MailSendException:邮件服务器连接失败;嵌套异常是 javax.mail.MessagingException:无法连接到 SMTP 主机:smtp.dwidasa.com,端口:587;嵌套异常是:java.net.ConnectException: Connection timed out(连接超时)。失败消息:javax.mail.MessagingException:无法连接到 SMTP 主机:smtp.dwidasa.com,端口:587;嵌套异常是:java.net.ConnectException: Connection timed out(连接超时);消息异常详细信息(1)是:

标签: javaspringsmtp

解决方案


尝试使用端口:587 并为 javaMailProperties 添加以下属性

<prop key="mail.transport.protocol">smtp</prop>

推荐阅读