- Home
- Testing
- GreenMail
- Basic Idea on GreenMail
Test Your Sending Code
- System testing an application. GreenMail responds like a regular SMTP server but does not deliver any email, which enables it to be used in real life applications and real test cases. Messages can easily be extracted, verified, and modified. Support for SMTPS (SSL) is enabled.
- GreenMail is an excellent choice for unit testing code that needs to send email with a succinct, efficient (non-polling) way to wait for messages to arrive, retrieve, verify, and modify messages.
- Note that retrieval of emails can be made with a simple java method or through a provided POP3, IMAP retriever helper class.
public void testYourSendingCode() throws Exception {
GreenMailgreenMail = new GreenMail(); //uses test ports by default
greenMail.start();
GreenMailUtil.sendTextEmailTest("to@localhost.com",
"from@localhost.com", "subject", "body"); //replace this with your send code
assertEquals("body", GreenMailUtil.getBody(greenMail.getReceivedMessages()[0]));
greenMail.stop();
//That's it!
}
Test Your Retrieving Code
* Again GreenMail can be used for system or unit testing an application needing to use POP3 or IMAP by responding like a standard compliant POP3 or IMAP server. Support for POP3S and IMAPS (SSL) is also enabled.
* Messages can be placed directly in users mailboxes or by using SMTP.
* GreenMail ships with helper classes for sending and retrieving. See the javadocs for the Retriever.java class
public void testYourRetrievingCode() throws Exception {
GreenMailgreenMail = new GreenMail(); //uses test ports by default
greenMail.start();
MimeMessage message = <CONSTRUCT MESSAGE>
User user = greenMail.setUser("to@localhost.com", "login-id", "password");
user.deliver(message);
GreenMailUtil.sendTextEmailTest("to@localhost.com", "from@localhost.com", subject, body);
assertEquals(2, greenMail.getReceivedMessages().length);
<PLACE YOUR POP3/IMAP RETRIVE CODE HERE>
}
GreenMail Maven Artifact
<dependency>
<groupId>com.icegreen</groupId>
<artifactId>greenmail</artifactId>
<version>1.3.1b</version>
</dependency>