Running JUnit test cases utilizing GreenMail to do some basic email validation. I'm having issues when attempting to validate the RecipientType.TO. My JUnit assertion is failing - so I logged the message header, and it appears that the recipient is actually being set twice.
Here is a sample unit test that I'm running:
@Autowired
private JavaMailSender mailSender;
private GreenMail greenMail;
@Before
public void setUp() {
// GreenMail config
greenMail = new GreenMail(ServerSetupTest.SMTP);
greenMail.start();
}
@Test
@DirtiesContext
public void testEmail() throws Exception {
SimpleMailMessage message = new SimpleMailMessage();
String testFrom = "from@test.com";
String testTo = "to@test.com";
String testSubject = "Test Subject";
String testText = "Test Text";
message.setFrom(testFrom);
message.setTo(testTo);
message.setSubject(testSubject);
message.setText(testText);
mailSender.send(message);
Message[] messages = greenMail.getReceivedMessages();
assertEquals(1, messages.length);
assertEquals(testSubject, messages[0].getSubject());
assertEquals(testText, GreenMailUtil.getBody(messages[0]));
assertEquals(testFrom, GreenMailUtil.getAddressList(messages[0].getFrom()));
assertEquals(testTo, GreenMailUtil.getAddressList(messages[0].getRecipients(Message.RecipientType.TO)));
}
I logged out the header and found that it contains two RecipientType.TO instances (Received, Message-ID, Date fields obfuscated):
Return-Path: <from@test.com>
Received: from localhost
Date: 12345678
From: from@test.com
To: to@test.com
To: to@test.com
Message-ID: <----------->
Subject: Test Subject
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Any ideas on why the To: looks to be set twice?
I've found that not using the GreenMailUtil.getAddresList(), explicitly returning the recipients and examining the first element works, for example:
Address[] recipientTo = messages[0].getRecipients(Message.RecipientType.TO);
assertEquals(testTo, recipientTo[0].toString());
But I'd like to try and understand why GreenMail looks to be setting the recipient address twice.
Cheers!
UPDATE
The above issue was run using GreenMail version 1.3.1b. As per suggestion of @Roland Weisleder updated to the latest version of GreenMail (1.5.3) and the same unit test runs as expected, setting the TO addressee only once.