如何使用 SMTP 为 SES 配置 Spring JavaMailSender?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8917400/
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-09-08 04:52:51  来源:igfitidea点击:

How to configure Spring JavaMailSender for SES using SMTP?

springsmtpamazon-sessmtps

提问by rreyes1979

We are trying to configure Spring JavaMailSender to work with Amazon's SES service using SMTP, but we are getting this error:

我们正在尝试将 Spring JavaMailSender 配置为使用 SMTP 与 Amazon 的 SES 服务一起使用,但我们收到此错误:

javax.mail.MessagingException: Could not connect to SMTP host: email-smtp.us-east-1.amazonaws.com, port: 465, response: -1

This is our configuration:

这是我们的配置:

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
    <property name="host" value="email-smtp.us-east-1.amazonaws.com" />
    <property name="port" value="465" />
    <property name="username" value="..." />
    <property name="password" value="..." />
    <property name="javaMailProperties">
        <props>
            <prop key="mail.smtp.auth">true</prop>
            <prop key="mail.smtp.ssl.enable">true</prop>
        </props>
    </property>
</bean>

Any ideas what could be wrong? Thanks in advance.

任何想法可能是错误的?提前致谢。

PS: We already tried the solution here: Could not connect to SMTP host: email-smtp.us-east-1.amazonaws.com, port: 465, response: -1without any luck.

PS:我们已经在这里尝试了解决方案:无法连接到 SMTP 主机:email-smtp.us-east-1.amazonaws.com,端口:465,响应:-1没有任何运气。

回答by rreyes1979

Based on @GuCo answer: This is the full configuration that worked for me:

基于@GuCo 回答:这是对我有用的完整配置:

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
    <property name="host" value="email-smtp.us-east-1.amazonaws.com" />
    <property name="port" value="465" />
    <property name="protocol" value="smtps" />
    <property name="username" value="..." />
    <property name="password" value="..." />
    <property name="javaMailProperties">
        <props>
            <prop key="mail.smtps.auth">true</prop>
            <prop key="mail.smtp.ssl.enable">true</prop>
            <prop key="mail.transport.protocol">smtps</prop>
        </props>
    </property>
</bean>

Do not forget the <property name="protocol" value="smtps" />configuration, or else the javaMailProperties are not taken into consideration.

不要忘记<property name="protocol" value="smtps" />配置,否则不会考虑 javaMailProperties。

回答by GuCo

I just came across the same problem. Actually, I tried to solve it a few weeks ago and got stuck ...

我刚刚遇到了同样的问题。实际上,几周前我试图解决它并被卡住了......

First thing I did, to identify the problem: activate the debugging mode for the mail api

我做的第一件事是找出问题:激活邮件 api 的调试模式

<props>
    ...
    <prop key="mail.debug">true</prop> 
</props>

This showed me, that it actually doesn't use SSL

这向我展示了它实际上不使用 SSL

DEBUG SMTP: trying to connect to host "email-smtp.us-east-1.amazonaws.com", port 465, isSSL false

My colleague pointed out, to include another mail property to really use SSL

我的同事指出,要包含另一个邮件属性才能真正使用 SSL

<props>
    ...
    <prop key="mail.transport.protocol">smtps</prop>
    ...
</props>

After adding this value the "isSSL" value changed to true, but pointed out another error. It doesn't use authentication anymore, because of the change of the protocol, which can be fixed by, of course, changing the property

添加此值后,“isSSL”值更改为 true,但指出了另一个错误。它不再使用身份验证,因为协议发生了变化,当然可以通过更改属性来修复

<prop key="mail.smtp.auth">true</prop>

to

<prop key="mail.smtps.auth">true</prop>

After that journey, it finally worked for me :-)

在那次旅程之后,它终于对我有用了:-)

Hope that was helpful ...

希望这是有帮助的...

Just to summarize the correct properties:

只是总结正确的属性:

<props>
    <prop key="mail.smtps.auth">true</prop>
    <prop key="mail.smtp.ssl.enable">true</prop>
    <prop key="mail.transport.protocol">smtps</prop>
</props>

回答by Nacho Mezzadra

This question is quite old, but in case someone needs the Spring boot configuration, this is what worked for me:

这个问题很老了,但如果有人需要 Spring 引导配置,这对我有用:

mail:
    host: email-smtp.us-west-2.amazonaws.com
    port: 465
    username: <username>
    password: <pwd>

    properties:
        mail.smtp.auth: true
        mail.smtp.starttls.enable: true
        mail.smtp.starttls.required: true
        mail.smtp.ssl.enable: true
        mail.transport.protocol: smtps
        mail.smtp.from: [email protected]

回答by Shivendra Namdeo

This code is working for me:

这段代码对我有用:

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
    <property name="host" value="email-smtp.us-east-1.amazonaws.com" />
            <!--Obtaining Your Amazon SES SMTP Credentials. use http://docs.aws.amazon.com/ses/latest/DeveloperGuide/smtp-credentials.html-->
    <property name="username" value="smtp user name" />
    <property name="password" value="smtp password" />
    <property name="javaMailProperties">
        <props>
            <prop key="mail.smtp.auth">true</prop>
            <prop key="mail.transport.protocol">smtp</prop>
            <prop key="mail.smtp.port">25</prop>
            <prop key="mail.smtp.starttls.enable">true</prop>
            <prop key="mail.smtp.starttls.required">true</prop>
            <prop key="mail.smtp.from">[email protected]</prop>
        </props>
    </property>
</bean>

回答by Sebastian

If you want to use @Bean, the following worked for me:

如果您想使用@Bean,以下内容对我有用:

@Bean
public MailSender mailSender() {
    JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
    mailSender.setHost("email-smtp.eu-west-1.amazonaws.com");
    mailSender.setUsername("...");
    mailSender.setPassword("...");
    mailSender.setPort(465);
    mailSender.setProtocol("smtps");

    // This can be very helpful
    Properties properties = new Properties();
    properties.setProperty("mail.debug", "true");
    mailSender.setJavaMailProperties(properties);

    return mailSender;
}