Java based mailserver

I am in need for a simple Java based mailserver. It should talk SMTP and IMAP, then I can use it to demo Ephesoft when out of network, and start capturing and extracting metadata from the attachments in my email.

I ran into Dwarf, actually a framework capable of doing a lot of other things next to being a mailserver. The protocols in use are configurable, it can talk IMAP, SMTP and POP3, although I am not planning on using the POP3. It even supports aliases!Once downloaded it will run on Linux as well as Windows, as long as you have some reasonably recent version of Java. Start it running %dwarf%/ (or run.bat). Running Ubuntu you might need to run it using sudo, because Ubuntu blocks access to ports under 1024 by default.

Tweak %dwarf%/config/main.xml, I configured it to run SMTP and IMAP protocols only:

<?xml version='1.0' encoding='us-ascii'?><!DOCTYPE configuration SYSTEM "file:lib/xmlConfiguration.dtd">
<configuration security="on" debug="imap" trace="imap" verbose="off">
 <property name="">file:conf/samples/dwarf/java.policy</property>
 <property name="">file:conf/samples/mail/jaas.policy</property>
 <property name="">file:conf/samples/dwarf/jaas.config</property>

 <!-- MAIN SERVER -->
 <service classbase="SK.gnome.dwarf" name="Main Server">
    <set name="logFacility">server</set>
    <set name="authFacility">Dwarf</set>

    <!-- CONSOLE -->
    <include url="file:conf/samples/mail/server/console.xml"/>

    <!-- LOG SERVER -->
    <include url="file:conf/samples/mail/server/logger.xml"/>

    <service name="JAAS Authenticator">
      <set name="logFacility">auth</set>
    <!-- IMAP SERVER -->
    <include url="file:conf/samples/mail/imap/imap.xml"/>
    <!-- SMTP SERVER -->
    <include url="file:conf/samples/mail/smtp/smtp.xml"/></pre>

I enable SMTP and IMAP.

In the respective config files (conf/samples/mail/smtp/smtp.xml (3 edits) and conf/samples/mail/imap/imap.xml (1 edit)) I changed the server location of localhost into localhost.domain.ext, to ensure that the mail does not accidentally end up at or any other uncontrolled location. In both files, I got:

<set name="hostId">localhost.domain.ext</set>

Remind to update the hosts file to assign (or any other relevant IP address) to localhost.domain.ext. I used the IP address of my VMWare guest OS.

Now we need to add users. the user stuff is stored in %dwarf%/conf/samples/dwarf/, a neat plain text file. You will find entries like:

# user: klaas
# full name: Klaas
# password: xxxxxxx (if non-real, then not valid login)
# account expiration: (0=no expiration)
# password expiration: (0=no expiration)
# roles: e.g. users,mail,deamon,administrator

The password can be generated at command line. Copy-paste it into the properties file (yes, advanced stuff!). To generate the encrypted password, run the following command:

./ conf/samples/dwarf/pwd/main.xml
This will allow you to encrypt any string into a password. If you’re into advanced stuff, you can define aliases in the file %dwarf%/conf/samples/mail/smtp/
Start your brand new mail server using the run command.
I connect using Thunderbird, sending mail through SMTP, receiving email using IMAP. I can create folders in IMAP, move email’s around, forward… Fully working! Nice, just what I needed.

Related projects worth mentioning
During my search and config, I also ran into GreenMail, a tool that looks really suitable for running JUnit to actually test if mail is sent/received. So it is kind of a tool that can mimick a mailserver, and allows JUnit asserts onto the mail server content.

Finally I noticed DevNullSMTP, a tool that allows email clients to post email, but as the name suggests, the mail goes nowhere. It just eats up all mail it receives, and puts it into ‘/dev/null’, the Unix/Linux equivalent for something like ‘lost forever’.

Two tools that can come in handy. Not for my current use, but who knows when I need one of them while developing…

[update 20110527: added syntax highlighting]