Best way to test email

One thing that can be difficult to test is email, get it wrong and at worst you can end up sending confusing and misleading emails to your clients.  Its easy to get a webserver and a local copy of the database set up, but what about a local email server.

One approach is to create your own mail function, which looks at a system environment to see if it should actually send the mail or not.  For instance, if it is a dev machine, ignore the mail function and write the contents of the email to log.  This is OK, but it doesn’t actually test the email function until it is on live, which is arguably too late.

I have just come across a package called Dumbster.  It is a java application that will sit and respond to SMTP requests, but instead of sending the email on to the recipient it holds on to it in an internal store.  Bingo, we are actually testing the mail function, but the email is being contained.

To get up and running you will need Java, and Ant, and a copy of the source code.  Extract the files and open up a terminal in the folder you placed the source.  Run ant and you should see something like this.

$ ant
Buildfile: /home/ubuntu/rjo1970-dumbster-d20b4e6/build.xml

clean:
[delete] Deleting directory /home/ubuntu/rjo1970-dumbster-d20b4e6/build

init:
[mkdir] Created dir: /home/ubuntu/rjo1970-dumbster-d20b4e6/build/classes
[mkdir] Created dir: /home/ubuntu/rjo1970-dumbster-d20b4e6/build/test
[mkdir] Created dir: /home/ubuntu/rjo1970-dumbster-d20b4e6/build/test/report

compile:
[javac] Compiling 29 source files to /home/ubuntu/rjo1970-dumbster-d20b4e6/build/classes

jar:
[jar] Building jar: /home/ubuntu/rjo1970-dumbster-d20b4e6/build/dumbster.jar

compile-tests:
[javac] Compiling 6 source files to /home/ubuntu/rjo1970-dumbster-d20b4e6/build/test

test:
[cobertura-instrument] Cobertura 1.9.4.1 - GNU GPL License (NO WARRANTY) - See COPYRIGHT file
[cobertura-instrument] Instrumenting 29 files to /home/ubuntu/rjo1970-dumbster-d20b4e6/build/instrumented
[cobertura-instrument] Cobertura: Saved information on 29 classes.
[cobertura-instrument] Instrument time: 174ms
[junit] Running com.dumbster.smtp.MailMessageTest
[junit] Tests run: 10, Failures: 0, Errors: 0, Time elapsed: 0.181 sec
[junit] Flushing results...
[junit] Flushing results done
[junit] Cobertura: Loaded information on 29 classes.
[junit] Cobertura: Saved information on 29 classes.
[junit] Running com.dumbster.smtp.RequestTest
[junit] Tests run: 38, Failures: 0, Errors: 0, Time elapsed: 0.079 sec
[junit] Flushing results...
[junit] Flushing results done
[junit] Cobertura: Loaded information on 29 classes.
[junit] Cobertura: Saved information on 29 classes.
[junit] Running com.dumbster.smtp.RollingMailStoreTest
[junit] Tests run: 7, Failures: 0, Errors: 0, Time elapsed: 0.049 sec
[junit] Flushing results...
[junit] Flushing results done
[junit] Cobertura: Loaded information on 29 classes.
[junit] Cobertura: Saved information on 29 classes.
[junit] Running com.dumbster.smtp.SmtpServerTest
[junit] Tests run: 10, Failures: 0, Errors: 0, Time elapsed: 1.113 sec
[junit] Flushing results...
[junit] Flushing results done
[junit] Cobertura: Loaded information on 29 classes.
[junit] Cobertura: Saved information on 29 classes.
[junit] Running com.dumbster.smtp.SmtpStateTest
[junit] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 0.038 sec
[junit] Flushing results...
[junit] Flushing results done
[junit] Cobertura: Loaded information on 29 classes.
[junit] Cobertura: Saved information on 29 classes.
[junitreport] Processing /home/ubuntu/rjo1970-dumbster-d20b4e6/build/test/report/TESTS-TestSuites.xml to /tmp/null279172976
[junitreport] Loading stylesheet jar:file:/usr/share/ant/lib/ant-junit.jar!/org/apache/tools/ant/taskdefs/optional/junit/xsl/junit-frames.xsl
[junitreport] Transform time: 701ms
[junitreport] Deleting: /tmp/null279172976
[cobertura-report] Cobertura 1.9.4.1 - GNU GPL License (NO WARRANTY) - See COPYRIGHT file
[cobertura-report] Cobertura: Loaded information on 29 classes.
[cobertura-report] Report time: 237ms

BUILD SUCCESSFUL
Total time: 10 seconds

You have now compiled the Java, and to run it use the following

java -jar build/dumbster.jar [PORT]

and substitute [PORT] for the port you want to run it on, or leave it empty to run it on port 25, the default for SMTP.

Next make sure that your development setup is configured to use Dumbster as its SMTP server and you are good to go.

Once you have run your tests, or done what you need to do, you can have a look at what Dumbster has collected.  They easiest way is to telnet into it.  Once you have connected you need to be polite and say HELO.  You’re then free to inspect your email.

To see how many emails Dumbster has collected simply type LIST into the terminal.  You should see something like this.

$ telnet localhost 25
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
220 localhost Dumbster SMTP service ready
HELO
250 OK
LIST
250 There are 2 message(s).

To see an individual message you need to specify the index of the message, with the numbering starting at 0.  You should see something like this.

LIST 0
250
-------------------------------------------
Subject: This is a subject
Date: Tue, 14 Feb 2012 09:10:57 +0000
To: blog@ghosty.co.uk
From: blog@ghosty

This is an interesting test message.
There are 2 message(s).

Once you are done, type the QUIT command so that you free the server up to run more tests.  The server will only allow a single connection at a time, and so if you leave your connection open, other applications will hang on the connection.

 


Posted

in

by

Tags:

Comments

Leave a Reply