GreenMail - 没有错误但没有收到消息

时间:2018-01-16 20:22:15

标签: testing greenmail

我正在尝试使用GreenMail来测试我的代码是否正确发送电子邮件。 我把所有的东西放在一个地方,这样我就可以在这里压缩和粘贴。

问题是GreenMail似乎没有收到电子邮件。

如果我用Session.getDefaultInstance(...)替换方法调用greenMail.getSmtps().createSession(),它似乎工作正常,但目前我没有很好的计划使会话可注入。

我做错了什么?

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("XXX@XXX.com"));
    message.setRecipients(Message.RecipientType.TO, 
            new InternetAddress[]{new InternetAddress("XXX@XXX.com")});
    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);

这是控制台日志。它似乎成功验证,我也没有发现任何错误。

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:<XXX@XXX.com>
250 Ok
RCPT TO:<XXX@XXX.com>
250 Ok
DEBUG SMTP: Verified Addresses
DEBUG SMTP:   XXX@XXX.com
DATA
354 End data with <CR><LF>.<CR><LF>
From: XXX@XXX.com
To: XXX@XXX.com
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 个答案:

答案 0 :(得分:0)

使用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());

甚至更好:

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