mercredi 27 mai 2015

Can't send mail using JavaMail and Amazon SES using SMTP

I am trying to send an email through Amazon SMTP using JavaMail (1.4.7).

Here's my code

import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class AmazonSESSample {

        static final String FROM = "noreply@corp.com";
        static final String TO = "me@corp.com";

        static final String BODY = "this is the body";
        static final String SUBJECT = "hello test";
        static final String USER = "...";
        static final String PASS = "...";

        // Port we will connect to on the Amazon SES SMTP endpoint. We are choosing port 25 because we will use
        // STARTTLS to encrypt the connection.
        static final int PORT = 465;
        static final String PROTOCOL = "smtps";
        static final String HOST = "email-smtp.us-west-2.amazonaws.com";

        public static void main(String[] args) throws Exception {

                // Create a Properties object to contain connection configuration information.
                Properties props = System.getProperties();
                props.put("mail.transport.protocol", PROTOCOL);
                props.put("mail.smtps.host", HOST);
                props.put("mail.smtps.port", PORT); 
                props.put("mail.smtps.user", FROM);
                props.put("mail.debug","true");

                // Set properties indicating that we want to use STARTTLS to encrypt the connection.
                // The SMTP session will begin on an unencrypted connection, and then the client
                // will issue a STARTTLS command to upgrade to an encrypted connection.
//        props.put("mail.smtps.auth", "true");
//        props.put("mail.smtp.starttls.enable", "true");
//        props.put("mail.smtp.starttls.required", "true");
//        props.put("mail.smtps.ssl.enable", "true");

                // Create a Session object to represent a mail session with the specified properties. 
                Session session = Session.getDefaultInstance(props);
                session.setDebug(true);

                // Create a message with the specified information. 
                MimeMessage msg = new MimeMessage(session);
                msg.setFrom(new InternetAddress(FROM));
                msg.setRecipient(Message.RecipientType.TO, new InternetAddress(TO));
                msg.setSubject(SUBJECT);
                msg.setContent(BODY,"text/plain");
                //msg.saveChanges();

                // Create a transport.        
                Transport transport = session.getTransport("smtps");

                // Send the message.
                try
                {
                        System.out.println("connecting...");

                        // Connect to Amazon SES using the SMTP username and password you specified above.
                        transport.connect(HOST, USER, PASS);
                        System.out.println("connected");
                        // Send the email.
                        System.out.println("sending...");
                        transport.sendMessage(msg, msg.getAllRecipients());

                        System.out.println("Email sent!");
                }
                catch (Exception ex) {
                        System.out.println("The email was not sent.");
                        System.out.println("Error message: " + ex.getMessage());
                }
                finally
                {
                        // Close and terminate the connection.
                        transport.close();          
                }
        }
}

I get two types of errors sporadically.

sometimes I get "421 Timeout waiting for data from client":

DEBUG: JavaMail version 1.4.7
DEBUG: URL jar:file:/Users/adavid/.m2/repository/com/amazonaws/aws-java-sdk-ses/1.9.23/aws-java-sdk-ses-1.9.23.jar!/META-INF/javamail.providers
DEBUG: Bad provider entry: 
DEBUG: successfully loaded resource: jar:file:/Users/adavid/.m2/repository/com/amazonaws/aws-java-sdk-ses/1.9.23/aws-java-sdk-ses-1.9.23.jar!/META-INF/javamail.providers
DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
DEBUG: Tables of loaded providers
DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle], com.amazonaws.services.simpleemail.AWSJavaMailTransport=javax.mail.Provider[TRANSPORT,aws,com.amazonaws.services.simpleemail.AWSJavaMailTransport,Amazon Web Services LLC]}
DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle], aws=javax.mail.Provider[TRANSPORT,aws,com.amazonaws.services.simpleemail.AWSJavaMailTransport,Amazon Web Services LLC]}
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
DEBUG: setDebug: JavaMail version 1.4.7
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle]
connecting...
DEBUG SMTP: useEhlo true, useAuth false
DEBUG SMTP: trying to connect to host "email-smtp.us-west-2.amazonaws.com", port 465, isSSL true
220 email-smtp.amazonaws.com ESMTP SimpleEmailService-1062959777 MTyV0IkyBqUpkEgbX2Av
DEBUG SMTP: connected to host "email-smtp.us-west-2.amazonaws.com", port: 465

EHLO 10.8.1.217
250-email-smtp.amazonaws.com
250-8BITMIME
250-SIZE 10485760
250-AUTH PLAIN LOGIN
250 Ok
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "SIZE", arg "10485760"
DEBUG SMTP: Found extension "AUTH", arg "PLAIN LOGIN"
DEBUG SMTP: Found extension "Ok", arg ""
DEBUG SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM 
DEBUG SMTP: AUTH LOGIN command trace suppressed
DEBUG SMTP: AUTH LOGIN succeeded
connected
sending...
DEBUG SMTP: use8bit false
MAIL FROM:<noreply@pingidentity.com>
250 Ok
RCPT TO:<adavid@pingidentity.com>
250 Ok
DEBUG SMTP: Verified Addresses
DEBUG SMTP:   adavid@pingidentity.com
DATA
354 End data with <CR><LF>.<CR><LF>
From: noreply@pingidentity.com
To: adavid@pingidentity.com
Message-ID: <1653497584.0.1432736817561.JavaMail.javamailuser@localhost>
Subject: hello test
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

this is the body
.
421 Timeout waiting for data from client.
DEBUG SMTP: got response code 421, with response: 421 Timeout waiting for data from client.

RSET
DEBUG SMTP: EOF: [EOF]
DEBUG SMTP: MessagingException while sending, THROW: 
com.sun.mail.smtp.SMTPSendFailedException: 421 Timeout waiting for data from client.

    at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2108)
    at com.sun.mail.smtp.SMTPTransport.finishData(SMTPTransport.java:1889)
    at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1120)
    at AmazonSESSample.main(AmazonSESSample.java:68)
The email was not sent.
Error message: 421 Timeout waiting for data from client.

QUIT
Exception in thread "main" javax.mail.MessagingException: Can't send command to SMTP host;
    nested exception is:
    java.net.SocketException: Connection closed by remote host
    at com.sun.mail.smtp.SMTPTransport.sendCommand(SMTPTransport.java:2157)
    at com.sun.mail.smtp.SMTPTransport.sendCommand(SMTPTransport.java:2144)
    at com.sun.mail.smtp.SMTPTransport.close(SMTPTransport.java:1210)
    at AmazonSESSample.main(AmazonSESSample.java:79)
Caused by: java.net.SocketException: Connection closed by remote host
    at sun.security.ssl.SSLSocketImpl.checkWrite(SSLSocketImpl.java:1490)
    at sun.security.ssl.AppOutputStream.write(AppOutputStream.java:70)
    at com.sun.mail.util.TraceOutputStream.write(TraceOutputStream.java:128)
    at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:82)
    at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:140)
    at com.sun.mail.smtp.SMTPTransport.sendCommand(SMTPTransport.java:2155)
    ... 3 more

and sometimes I get "EOF: [EOF]":

    DEBUG: JavaMail version 1.4.7
DEBUG: URL jar:file:/Users/adavid/.m2/repository/com/amazonaws/aws-java-sdk-ses/1.9.23/aws-java-sdk-ses-1.9.23.jar!/META-INF/javamail.providers
DEBUG: Bad provider entry: 
DEBUG: successfully loaded resource: jar:file:/Users/adavid/.m2/repository/com/amazonaws/aws-java-sdk-ses/1.9.23/aws-java-sdk-ses-1.9.23.jar!/META-INF/javamail.providers
DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
DEBUG: Tables of loaded providers
DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle], com.amazonaws.services.simpleemail.AWSJavaMailTransport=javax.mail.Provider[TRANSPORT,aws,com.amazonaws.services.simpleemail.AWSJavaMailTransport,Amazon Web Services LLC]}
DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle], aws=javax.mail.Provider[TRANSPORT,aws,com.amazonaws.services.simpleemail.AWSJavaMailTransport,Amazon Web Services LLC]}
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
DEBUG: setDebug: JavaMail version 1.4.7
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle]
connecting...
DEBUG SMTP: useEhlo true, useAuth false
DEBUG SMTP: trying to connect to host "email-smtp.us-west-2.amazonaws.com", port 465, isSSL true
220 email-smtp.amazonaws.com ESMTP SimpleEmailService-1062893701 2HRiJbNHu9DYCQClBXLu
DEBUG SMTP: connected to host "email-smtp.us-west-2.amazonaws.com", port: 465

EHLO 10.8.1.217
421 Timeout waiting for data from client.
HELO 10.8.1.217
DEBUG SMTP: EOF: [EOF]
The email was not sent.
Error message: [EOF]

I'm missing something and I don't know what...

would appreciate any help with this




Aucun commentaire:

Enregistrer un commentaire