質問

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

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: -1 without any luck.

役に立ちましたか?

解決

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

<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.

他のヒント

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

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

This showed me, that it actually doesn't use 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

<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

<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>

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

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: no-reply@yourdomain.com

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">abc@example.com</prop>
        </props>
    </property>
</bean>

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

@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;
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top