質問

I am trying to send email with Amazon's SES/SMTP and I am getting the following error:

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

Here is how I am trying to send the mail:

Spring mail sender config:

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host" value="${mail.server}"/>
        <property name="port" value="${mail.port}"/>
        <property name="username" value="${aws.mail.smtp.user}"/>
        <property name="password" value="${aws.mail.smtp.password}"/>
        <property name="javaMailProperties">
            <props>
            <!-- Use SMTP-AUTH to authenticate to SMTP server -->
            <prop key="mail.smtp.auth">true</prop>
            <!-- Use TLS to encrypt communication with SMTP server -->
            <prop key="mail.smtp.starttls.enable">true</prop>  
            </props>    
        </property>
    </bean>

with:

mail.server =email-smtp.us-east-1.amazonaws.com
mail.port = 465
役に立ちましたか?

解決

With amazon SES, configuration needs to be as follows:

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

instead of:

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

as hinted by dave.

EDIT: Please use this solution: https://stackoverflow.com/a/8928559/536299

他のヒント

Amazon SES SMTP requires the SSL before the SMTP session. The StartTLS command is not supported by SES.

These settings worked for me:

mail.transport.protocol=smtp
mail.smtp.port=25
mail.smtp.auth=true
mail.smtp.starttls.enable=true
mail.smtp.starttls.required=true
mail.smtp.host=email-smtp.us-east-1.amazonaws.com
mail.smtp.user=[SMTP username]
mail.smtp.password=[SMTP user password]

If you try to connect to connect using SSL connection, it rejected the connection. So you need to do STARTTLS after connection.

You can add mail.debug=true to see where it failed.

The sender email address must be a verified email address otherwise SES refuses to forward the email.

I had the same issue with port 25, but for me it worked with port 587.

This employee from AWS states that SES does not support SSL at all. https://forums.aws.amazon.com/message.jspa?messageID=218303 .

Amazon SES will attempt to send email with Transport Layer Security enabled, but there is not a way to guarantee messages are sent with TLS. SES uses opportunistic TLS when sending emails, which means it will attempt to send emails over TLS first, and then will fall back to regular SMTP if TLS is unavailable.

Hence, I am thinking the issue you are seeing is not TLS or SSL related, rather something else.

Note that the AWS note at https://forums.aws.amazon.com/message.jspa?messageID=218303 refers to encrypting server-to-server communication to maintain confidentiality of the email message, is a shared characteristic of all SMTP services.

This question relates to using a secure connection to the AWS SMTP server to protect the passwords used to authenticate with the AWS server.

Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.host", "email-smtp.us-east-1.amazonaws.com");
props.setProperty("mail.user", "your_ses_user");
props.setProperty("mail.password", "your_ses_pwd");



Session mailSession = Session.getDefaultInstance(props, new Authenticator(){
    public PasswordAuthentication getPasswordAuthentication() {
        String username = "your_ses_user";
        String password = "your_ses_pwd";
        return new PasswordAuthentication(username, password);
    }
});

These code have been tested, works fine well. If you want use SMTP over SSL, please config:

props.setProperty("mail.smtp.starttls.enable", "true");

Or you can download AWS Java SDK from HERE.

Code sample is HERE

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top