Тест интеграции GreenMail отправил письмо, не передаваемое получателю - PullRequest
0 голосов
/ 12 июня 2018

Я использую greenmail для тестирования интеграции почты, и вышел с кодом ниже.Намерение состоит в том, чтобы создать 2 поддельных почтовых сервера, которые будут выполнять функции отправителя и получателя.Однако, когда я запустил код, тест не прошел, потому что получатель не получил письмо.Я прочитал некоторые примеры и часто задаваемые вопросы с сайта 'greenmail', но не могу найти объяснения этому поведению.Я надеюсь, что кто-то мог бы пролить некоторый свет здесь.

Ниже приведен тестовый код:

    @Before
public void setUp() {

    // Setup fake smtp server.
    ServerSetup smtpServer = new ServerSetup(Integer.parseInt(smtpPort), smtpHost, smtpProtocol);
    serverA = new GreenMail(smtpServer);
    serverA.start();

    // Setup fake pop3 server.
    ServerSetup pop3Server = new ServerSetup(Integer.parseInt(pop3Port), pop3Host, pop3Protocol);
    serverB = new GreenMail(pop3Server);
    serverB.start();
}

@Test
public void testSendEmailToAvailableMailboxReturnEmail() {
    String senderMail = "sender.1@testmail.com";
    String senderUsername = "sender1";
    String senderPassword = "password123";

    String receiverMail = "receiver.1@testmail.com";
    String receiverUsername = "receiver1";
    String receiverPassword = "pass567";

    // Set mailbox for sender and receiver
    serverA.setUser(senderMail, senderUsername, senderPassword);
    serverB.setUser(receiverMail, receiverUsername, receiverPassword);

    // Send email from serverA
    String subject = "This is a test subject.";
    String msg = "This is a plain test message.";

    EmailModel emailModel = new EmailModel();
    emailModel.setFrom(senderMail);
    emailModel.setTo(receiverMail);
    emailModel.setSubject(subject);
    emailModel.setBody(msg);
    emailModel.setSentDate(LocalDateTime.now());
    emailModel.setBodyType(EmailModel.EMAIL_TYPE_TEXT);

    // Using javamailsenderimpl to send the mail.
    mailServiceImpl.send(emailModel);

    // Retrieve email from ServerB
    Session session = Session.getDefaultInstance(new Properties(), null);
    try {
        Store store = session.getStore(pop3Protocol);
        store.connect(pop3Host, receiverUsername, receiverPassword);

        Folder inbox = store.getFolder(BatchConstant.MAIL_INBOX);
        inbox.open(Folder.READ_ONLY);
        Message[] messages = inbox.getMessages();
        Assert.assertNotNull(messages);
        Assert.assertEquals(1, messages.length);
        Assert.assertEquals(subject, messages[0].getSubject());
        Assert.assertEquals(msg, String.valueOf(messages[0].getContent()).contains(msg));

    } catch (MessagingException | IOException e) {
        Assert.fail("Should be able to retrive sent mail.");
    }


}

@After
public void tearDown() {
    serverA.stop();
    serverB.stop();
}

Сбой в этой строке:

Assert.assertEquals(1, messages.length);

Журнал:

23:42:57.998 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.boot.test.mock.mockito.MockitoBeans'
23:42:58.014 [main] DEBUG com.icegreen.greenmail.util.GreenMail - Started services, performing check if all up
23:42:58.014 [smtp:127.111.44.233:25] DEBUG com.icegreen.greenmail.smtp.SmtpServer - Started smtp:127.111.44.233:25
23:42:58.029 [main] DEBUG com.icegreen.greenmail.util.GreenMail - Started services, performing check if all up
23:42:58.029 [pop3:127.124.244.10:110] DEBUG com.icegreen.greenmail.pop3.Pop3Server - Started pop3:127.124.244.10:110
23:42:58.061 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'getJavaMailSender'
DEBUG: JavaMail version 1.5.6
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]}
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]}
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
DEBUG SMTP: useEhlo true, useAuth false
DEBUG SMTP: trying to connect to host "127.111.44.233", port 25, isSSL false
23:42:58.170 [smtp:127.111.44.233:25] DEBUG com.icegreen.greenmail.smtp.SmtpServer - Handling new client connection smtp:127.111.44.233:25<-/127.0.0.1:55846
220 /127.111.44.233 GreenMail SMTP Service v1.5.7 ready
DEBUG SMTP: connected to host "127.111.44.233", port: 25

EHLO MXXXXXXXXX
250 /127.111.44.233
DEBUG SMTP: use8bit false
MAIL FROM:<sender.1@testmail.com>
250 OK
RCPT TO:<receiver.1@testmail.com>
250 OK
DEBUG SMTP: Verified Addresses
DEBUG SMTP:   receiver.1@testmail.com
DATA
354 Start mail input; end with <CRLF>.<CRLF>
Date: Tue, 12 Jun 2018 23:42:58 +0800 (SGT)
From: sender.1@testmail.com
To: receiver.1@testmail.com
Message-ID: <2074658615.2.1528818178201@MXXXXXXXXX>
Subject: This is a test subject.
MIME-Version: 1.0
Content-Type: multipart/mixed; 
    boundary="----=_Part_0_52514534.1528818178108"

------=_Part_0_52514534.1528818178108
Content-Type: multipart/related; 
    boundary="----=_Part_1_1046665075.1528818178123"

------=_Part_1_1046665075.1528818178123
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit

This is a plain test message.
------=_Part_1_1046665075.1528818178123--

------=_Part_0_52514534.1528818178108--
.
23:42:58.217 [smtp:127.111.44.233:25<-/127.0.0.1:55846] INFO com.icegreen.greenmail.smtp.SmtpManager - Created user login receiver.1@testmail.com for address receiver.1@testmail.com with password receiver.1@testmail.com because it didn't exist before.
250 OK
DEBUG SMTP: message successfully delivered to mail server
QUIT
221 /127.111.44.233 Service closing transmission channel
23:42:58.264 [pop3:127.124.244.10:110] DEBUG com.icegreen.greenmail.pop3.Pop3Server - Handling new client connection pop3:127.124.244.10:110<-/127.0.0.1:55847
23:42:58.264 [pop3:127.124.244.10:110<-/127.0.0.1:55847] DEBUG com.icegreen.greenmail.pop3.Pop3Connection - S: +OK POP3 GreenMail Server v1.5.7 ready
23:42:58.279 [pop3:127.124.244.10:110<-/127.0.0.1:55847] DEBUG com.icegreen.greenmail.pop3.Pop3Connection - C: CAPA
23:42:58.279 [pop3:127.124.244.10:110<-/127.0.0.1:55847] DEBUG com.icegreen.greenmail.pop3.Pop3Connection - S: +OK
23:42:58.279 [pop3:127.124.244.10:110<-/127.0.0.1:55847] DEBUG com.icegreen.greenmail.pop3.Pop3Connection - S: UIDL
23:42:58.279 [pop3:127.124.244.10:110<-/127.0.0.1:55847] DEBUG com.icegreen.greenmail.pop3.Pop3Connection - S: .
23:42:58.279 [pop3:127.124.244.10:110<-/127.0.0.1:55847] DEBUG com.icegreen.greenmail.pop3.Pop3Connection - C: USER receiver1
23:42:58.279 [pop3:127.124.244.10:110<-/127.0.0.1:55847] DEBUG com.icegreen.greenmail.pop3.Pop3Connection - S: +OK
23:42:58.279 [pop3:127.124.244.10:110<-/127.0.0.1:55847] DEBUG com.icegreen.greenmail.pop3.Pop3Connection - C: PASS pass567
23:42:58.279 [pop3:127.124.244.10:110<-/127.0.0.1:55847] DEBUG com.icegreen.greenmail.pop3.Pop3Connection - S: +OK
23:42:58.279 [pop3:127.124.244.10:110<-/127.0.0.1:55847] DEBUG com.icegreen.greenmail.pop3.Pop3Connection - C: STAT
23:42:58.279 [pop3:127.124.244.10:110<-/127.0.0.1:55847] DEBUG com.icegreen.greenmail.pop3.Pop3Connection - S: +OK 0 0
23:42:58.279 [pop3:127.124.244.10:110<-/127.0.0.1:55847] DEBUG com.icegreen.greenmail.pop3.Pop3Connection - C: NOOP
23:42:58.279 [pop3:127.124.244.10:110<-/127.0.0.1:55847] DEBUG com.icegreen.greenmail.pop3.Pop3Connection - S: +OK noop rimes with poop
23:42:58.279 [main] DEBUG com.icegreen.greenmail.util.GreenMail - Stopping GreenMail ...
23:42:58.279 [main] DEBUG com.icegreen.greenmail.util.GreenMail - Stopping service smtp:127.111.44.233:25
23:42:58.295 [main] DEBUG com.icegreen.greenmail.smtp.SmtpServer - Stopping smtp:127.111.44.233:25
23:42:58.295 [main] DEBUG com.icegreen.greenmail.smtp.SmtpServer - Stopped smtp:127.111.44.233:25
23:42:58.295 [main] DEBUG com.icegreen.greenmail.util.GreenMail - Stopping GreenMail ...
23:42:58.295 [main] DEBUG com.icegreen.greenmail.util.GreenMail - Stopping service pop3:127.124.244.10:110
23:42:58.295 [main] DEBUG com.icegreen.greenmail.pop3.Pop3Server - Stopping pop3:127.124.244.10:110
23:42:58.295 [main] DEBUG com.icegreen.greenmail.pop3.Pop3Server - Stopped pop3:127.124.244.10:110
23:42:58.295 [main] DEBUG org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate - Retrieved ApplicationContext from cache with key [[MergedContextConfiguration@4e7dc304 testClass = MailServiceImplTest, locations = '{}', classes = '{class com.xxx.xoxoxo.mail.service.impl.MailServiceImpl, class com.xxx.xoxoxo.config.MailServiceConfig, class com.xxx.xoxoxo.batch.encryption.AesEncryptor, class com.xxx.xoxoxo.config.ApplicationProperties}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{enote.mail.host=127.111.44.233, enote.mail.port=25, enote.mail.protocol=smtp, enote.mail.encoding=UTF-8, enote.mail.auth=false, enote.mail.username=, enote.mail.password=}', contextCustomizers = set[org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@59f99ea, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@47c62251, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@589838eb], contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader', parent = [null]]]
23:42:58.295 [main] DEBUG org.springframework.test.context.cache - Spring test ApplicationContext cache statistics: [DefaultContextCache@645aa696 size = 1, maxSize = 32, parentContextCount = 0, hitCount = 2, missCount = 1]

1 Ответ

0 голосов
/ 09 июля 2018

GreenMail - это сервер песочницы.Он никогда не будет направлять почту на другой сервер.

Вам нужно создать один экземпляр GreenMail, настроенный для SMTP и POP3:

 ServerSetup smtpServerSetup = ...
 ServerSetup pop3ServerSetup = ...
 GreenMail greenMail = new GreenMail(new ServerSetup[]{smtpServerSetup, pop3ServerSetup});
 greenMail.start(); // Starts both smtp and pop3
...