GreenMail - no error but no received messages

1.4k views Asked by At

I am trying to use GreenMail to test if my code is sending an email correctly. I moved all the pieces in one place so that I can compact and paste here.

The problem is that GreenMail seems not to receiving the email.

If I replace the method call Session.getDefaultInstance(...) with greenMail.getSmtps().createSession() it seems to work fine but for the moment I have no nice plan to make the Session injectable.

What am I doing wrong?

Security.setProperty("ssl.SocketFactory.provider", 
        DummySSLSocketFactory.class.getTypeName());
GreenMail greenMail = new GreenMail(ServerSetup.SMTPS);
greenMail.start();
{
    // This is the test subject that I moved here to compact the code
    Security.addProvider(new Provider());

    Properties props = new Properties();
    props.put("mail.smtp.host", "email-smtp.XXX.amazonaws.com");
    props.put("mail.smtp.auth", "true");
    props.put("mail.debug", "true");
    props.put("mail.smtp.port", "465");
    props.put("mail.smtp.socketFactory.port", "465");
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.socketFactory.fallback", "false");

    Session session = Session.getDefaultInstance(props,
            new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication("XXX", "XXX");
                }
            });
    session.setDebug(true);

    Message message = new MimeMessage(session);
    message.setFrom(new InternetAddress("[email protected]"));
    message.setRecipients(Message.RecipientType.TO, 
            new InternetAddress[]{new InternetAddress("[email protected]")});
    message.setSubject("XXX");
    message.addHeader("Content-type", "text/html; charset=UTF-8");
    message.setContent("XXX", "text/html");

    Transport.send(message);
}
assertEquals(greenMail.getReceivedMessages().length, 1);

Here is the console log. It seems to authenticate sucessfully, also I am not finding any errors.

DEBUG: JavaMail version ${mail.version}
DEBUG: URL jar:file:/C:/XXX/.m2/repository/com/amazonaws/aws-java-sdk/1.7.1/aws-java-sdk-1.7.1.jar!/META-INF/javamail.providers
DEBUG: Bad provider entry: 
DEBUG: successfully loaded resource: jar:file:/C:/XXX/.m2/repository/com/amazonaws/aws-java-sdk/1.7.1/aws-java-sdk-1.7.1.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,Sun Microsystems, Inc], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc], 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,Sun Microsystems, Inc], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc], 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 ${mail.version}
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "email-smtp.XXX.amazonaws.com", port 465, isSSL false
220 email-smtp.amazonaws.com ESMTP SimpleEmailService-XXX XXX
DEBUG SMTP: connected to host "email-smtp.XXX.amazonaws.com", port: 465

EHLO X.X.X.X
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
DEBUG SMTP: check mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM 
AUTH LOGIN
334 XXX
XXX=
334 XXX
XXX=
235 Authentication successful.
DEBUG SMTP: use8bit false
MAIL FROM:<[email protected]>
250 Ok
RCPT TO:<[email protected]>
250 Ok
DEBUG SMTP: Verified Addresses
DEBUG SMTP:   [email protected]
DATA
354 End data with <CR><LF>.<CR><LF>
From: [email protected]
To: [email protected]
Message-ID: <X.0.X.JavaMail.XXX@XXX>
Subject: XXX
MIME-Version: 1.0
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit

XXX
.
250 Ok X-X-X-X-X-X-000000
QUIT
221 Bye
1

There are 1 answers

0
Marcel On

Use the preset config from ServerSetup.SMTPS:

GreenMail greenMail = new GreenMail(ServerSetup.SMTPS);
...
props.put("mail.smtp.host", ServerSetup.SMTPS.getBindAddress());
props.put("mail.smtp.port", ServerSetup.SMTPS.getPort());
props.put("mail.smtp.socketFactory.port", ServerSetup.SMTPS.getPort());

or even better:

Properties props = ServerSetup.SMTPS.configureJavaMailSessionProperties(null);