greenmail
greenmail copied to clipboard
Mails with multiple recipients
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("[email protected],[email protected]", "from@localhost", "subject", "body");
, I observe the following behaviour:
-
greenMail.getReceivedMessages()
-> returns two results -
getReceivedMessagesForDomain("a.com")
-> returns two results -
getReceivedMessagesForDomain("b.com")
-> returns two results
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?
Thx, @mpschaeuble
You could use IMAP connection to fetch mails for a specific user:
try (final IMAPStore store = greenMail.getImap().createStore()) {
store.connect("[email protected]", "[email protected]"); // Login as [email protected]
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 '[email protected]'
assertThat(GreenMailUtil.getAddressList(messages[0].getRecipients(Message.RecipientType.TO)))
.isEqualTo("[email protected], [email protected]");
}
I'll have to check about each delivered mail about keeping both recipients.