GreenMail - Email Test Framework in Java

In any project there will be a need to send mail out to users. It could be an alert mail, forget password or authentication related mail. Mail is the default communication between the software and the users. As a developer, we can write code, to send out a mail but we need to make sure whether it got successfully received and how the body of mail, Is it the same like what we have sent. GreenMail is a Email test framework which helps to send and receive mails. It is a test framework which supports SMTP, POP3, IMAP including SSL.
Automation is the buzz word every organization is using and pushing its developers to automate the code. Few organization use continous delivery to automate the build, which inturn will also performs unit testing. There is a requirement to have dummy / fake email server to send and receive the mails. We cannot afford to have actual mail server as the mails should not be sent out of the organization. There is always a requirement for a light weight email server which can send and receive mails and it is exclusively used for testing purposes. GreenMail is a perfect fit for that.
GreenMail Email mail server can started as standalone server. Download the greenmail.jar and execute the below command line to start the server.
java -Dgreenmail.setup.all -jar greenmail-standalone-1.5.7.jar
GreenMail server can be started and stopped programtically.
GreenMail greenMail = new GreenMail(ServerSetupTest.ALL);
greenMail.start(); //start the server
greenMail.stop(); //stop the server
We looked in to the library and personally liked it. Below is the sample to send and receive mail. We have used Javamail to send the mail and used GreenMail library to receive it.
Maven dependency
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>com.icegreen</groupId>
<artifactId>greenmail</artifactId>
<version>1.5.7</version>
</dependency>
Code to send mail using Javamail.
public static void sendMail() {
Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.host", "localhost");
props.put("mail.smtp.port", "25");
Session mailSession = Session.getInstance(props, null);
try {
MimeMessage msg = new MimeMessage(mailSession);
msg.setFrom("admin@example.com");
msg.setRecipients(Message.RecipientType.TO, "editor@findbestopensource.com");
msg.setSubject("Forget login request");
msg.setText("We got request that you want to reset your password. Below is the link ... ");
msg.setSentDate(new Date());
Transport.send(msg);
}
catch (MessagingException e) {
e.printStackTrace();
}
}
Code to receive mail using GreenMail library
public static void receiveMail() {
try {
GreenMail greenMail = new GreenMail(ServerSetup.SMTP_IMAP);
ServerSetup server = greenMail.getSmtp().getServerSetup();
System.out.println(server.getPort());
IMAPStore imapStore = greenMail.getImap().createStore();
imapStore.connect("editor@findbestopensource.com", "editor@findbestopensource.com");
Folder inbox = imapStore.getFolder("INBOX");
inbox.open(Folder.READ_WRITE);
int totalMsgCount = inbox.getMessageCount();
for(int i=1; i <= totalMsgCount; i++) {
Message msgReceived = inbox.getMessage(i);
System.out.println(msgReceived.getSubject());
}
inbox.close();
}
catch(Exception exp) {
exp.printStackTrace();
}
}
GreenMail library is a useful library for the one who wants to vaidate the mails being sent and those who want a test mail server.
References:
http://www.icegreen.com/greenmail
https://github.com/greenmail-mail-test/greenmail