Testing SMTP and IMAP with GreenMail

If you want to write some SMTP, POP3 or IMAP client code, maybe you’ll consider using GreenMail. This is a Java library that implements a mail server, and you can easily use it for testing purposes. You include it in your project, and in your tests, you can just start the server, send fake messages to it, or fetch them. You can connect to it and directly test IMAP, POP3, SMTP and their secure versions.

You can do something like this:

// ALL means you start all the protocols / servers 
// (SMTP, IMAP...) but you can choose only
// some of them.

GreenMail server = new GreenMail(ServerSetup.ALL);

/* Do your stuff */


“Your stuff” can be sending mail to your local host, and then checking. You can create a user before sending him an email:

// Creating the user
// Sending him an email
    	"First message subject",
	"This is the first body");

but if you don’t, a mailbox is automatically created when an email is received. Cool.

There is one annoying problem. In theory, according to documentation, GreenMail listens to port 3025 for SMTP. But when you try the above code, you find that mail sending is not working, and you get a connection refused exception.

Then you add a rule to your firewall to make sure port 3025 is allowed, to no avail.

And finally, in my case, a look at netstat or tcpview or whatever shows that GreenMail SMTP is listening to port 25, just as usual in any other SMTP server, in spite of what the documentation says. GreenMailUtil.sendTextEmailTest(), on the other hand, seems to be trying to connect to 3025, so it doesn’t match.

I’m not sure why this is happening. But my solution was to explicitly create my SMTP server at port 3025. Instead of using the predefined ServerSetup.ALL or other constants, you can be more explicit and create an array of ServerSetup objects, specifying port, address and protocol. So I changed the above code and created my servers like this:

ServerSetup[] ss = {
    	new ServerSetup(

server = new GreenMail(ss);

Notice that the string for the protocol “smtp” MUST be lowercase. In this case I use IMAP as well because I wanted to test IMAP, but it’s up to you which serversetups you include in that list. The IMAP predefined one worked well for me, so I didn’t explicitly create a new object for that one.

I’m not sure whether all of this is in the documentation, which is far from perfect. I had to find it myself just by trying and debugging.

Of course, that “3025” is here to make it clearly visible in the example; in your production code, you should use some constant that you can easily change just in case future versions of GreenMail switch to a different port 🙂 The same considerations apply to other constants in the code, which you should, as usual, subject to your coding expertise. Just in case.

Una respuesta to “Testing SMTP and IMAP with GreenMail”

  1. srmafias Says:

    Been there, done that 🙂
    The key issue here is (and the Javadoc documents it well) that when you build the GreenMail server, the first parameter defines the ports and protocols. If you pass a ServerSetup.ALL, it listens on 25, if you pass a ServerSetupTest.ALL, the ports have a 3000 offset 😉

    A great library for mail, otherwise. I tried it before ending with mailcatcher (http://mailcatcher.me), but only because I really like ruby…


Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión /  Cambiar )

Google photo

Estás comentando usando tu cuenta de Google. Cerrar sesión /  Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión /  Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión /  Cambiar )

Conectando a %s

A %d blogueros les gusta esto: