java spring - 配置 spring 电子邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1956506/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 18:42:42  来源:igfitidea点击:

spring - config spring email

javaspringemailjavamail

提问by robinmag

I used the springmail to send email from my smtp server with the following config:

我使用 springmail 从我的 smtp 服务器发送电子邮件,配置如下:

<bean id="springEmailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
    <property name="defaultEncoding" value="UTF-8"/>
    <property name="host" value="mail.myserver.com"/>
    <property name="port" value="25"/>

    <property name="username" value="username"/>
    <property name="password" value="password"/>
    <property name="javaMailProperties">
        <value> 
            mail.debug=true 
            mail.smtp.auth=true
            mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
            mail.smtp.socketFactory.fallback=false 
        </value>
    </property></bean>

But it throw "javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?" I've tested this config with gmail on port 465 and it worked.

但它抛出“javax.net.ssl.SSLException:无法识别的 SSL 消息,纯文本连接?” 我已经在端口 465 上使用 gmail 测试了这个配置并且它有效。

Please tell me what i've done wrong. Thank you

请告诉我我做错了什么。谢谢

回答by maximdim

It looks like your SMTP server requires SSL (secure) connection. See example below of how to configure it for Gmail, which is also requires SSL. Note smtps protocol and extra properties.

看起来您的 SMTP 服务器需要 SSL(安全)连接。请参阅下面的示例,了解如何为 Gmail 配置它,这也需要 SSL。注意 smtps 协议和额外的属性。

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
    <property name="host" value="smtp.gmail.com" />
    <property name="port" value="465" />
    <property name="protocol" value="smtps" />
    <property name="username" value="user"/>
    <property name="password" value="password"/>
    <property name="javaMailProperties">
        <props>
            <prop key="mail.smtps.auth">true</prop>
            <prop key="mail.smtps.starttls.enable">true</prop>
            <prop key="mail.smtps.debug">true</prop>
        </props>
    </property>
</bean>

回答by duffymo

Maybe you shouldn't be using SSL. Is the mail server configured to accept an encrypted message? Looks like it wants plain text.

也许您不应该使用 SSL。邮件服务器是否配置为接受加密邮件?看起来它想要纯文本。

I'd go back to the reference docsand see if that will work.

我会回到参考文档,看看这是否可行。

回答by Hyman Leow

I would try removing all javaMailProperties, as well as the username and password properties.

我会尝试删除所有 javaMailProperties,以及用户名和密码属性。

As duffymo points out, you probably shouldn't use SSL (port 25 is the non-SSL SMTP port). Most SMTP servers also don't require authentication (unless you've explicitly configured it).

正如 duffymo 指出的那样,您可能不应该使用 SSL(端口 25 是非 SSL SMTP 端口)。大多数 SMTP 服务器也不需要身份验证(除非您已明确配置它)。