greenmail-mail-test / greenmail

Official master for the Greenmail project
http://greenmail-mail-test.github.io/greenmail/
Apache License 2.0
627 stars 179 forks source link

Mails with multiple recipients #675

Open mpschaeuble opened 7 months ago

mpschaeuble commented 7 months ago

I'm currently using Greenmail 2.0.1 in JUnit5 tests and I'm not sure whether it's a bug or this is the expected behaviour: When sending a single mail to two different recipients, e.g. using GreenMailUtil.sendTextEmailTest("first@a.com,second@b.com", "from@localhost", "subject", "body");, I observe the following behaviour:

I would either expect getReceivedMessages() to return a single result (because it's one mail) or both getReceivedMessagesForDomain(...) to return a single result in this case. Is my expectation wrong? How can I verify a single mail is sent by my application under test to two different recipients?

marcelmay commented 7 months ago

Thx, @mpschaeuble

You could use IMAP connection to fetch mails for a specific user:

        try (final IMAPStore store = greenMail.getImap().createStore()) {
            store.connect("first@a.com", "first@a.com"); // Login as first@a.com

            Folder inboxFolder = store.getFolder("INBOX");
            inboxFolder.open(Folder.READ_ONLY);
            final Message[] messages = inboxFolder.getMessages();
            assertThat(messages).hasSize(1);

            // Unsure: Should mail keep both recipients, or only receiving 'first@a.com'
            assertThat(GreenMailUtil.getAddressList(messages[0].getRecipients(Message.RecipientType.TO)))               
                .isEqualTo("first@a.com, second@b.com");
        }

I'll have to check about each delivered mail about keeping both recipients.