Acorel
Gratis demo

Fake SMTP servers for testing

Azzeddine Daddah, 02 maart 2022

If you’re a developer you maybe have experienced this situation before. You’ve built a nice piece of functionality (let’s say an order confirmation email service). Testing that the received email looks as expected and includes the right data can be challenging. Setting up a local SMTP server can be difficult.

smtp server how it works wiki commons
– How an SMTP server works (Source: Wikimedia Commons)

 

One of the quickest and simplest approaches that I’ve seen developers doing when they want to test their code sending and receiving e-mails in a development environment is by configuring a ‘real’ email address as mail receiver. They use their personal e-mail address, set up a new one or use a temporary e-mail address provided by services like mail.tm.

This approach may help in some simple environments and cases, but sometimes a developer can deal with sensitive data that he doesn’t want to be exposed to an external email service or even when setting the email address is not possible. So:

If the answer is yes, then this article may help you. I’ll show you four tools which can help you run and test you code locally without actually sending emails to any outside address.

1. FakeSMTP

FakeSMTP may be one of the most popular mock SMTP servers used in development for testing emails. FakeSMTP catches any emails sent to it. Those emails can be accessed in its simple an intuitive GUI.

To start using fakeSMTP, you should first download the software here. The downloaded zip file contains a Java jar file which can be directly executed without the need for any installation.

Start the application from the command line and configure the port your (web)application will listen to. This can be done either by passing the port as an argument in the command line or via the GUI.

java -jar fakeSMTP.jar -p 2525

We start fakeSMTP on port 2525. By configuring your application to use “localhost” as your SMTP server, all emails will be intercepted and displayed.

Run your code which sends the email and check the result in fakeSMTP.

fakesmtp inbox
fakeSMTP GUI with email

We can see the email sent by us. By double-clicking the mail, it will be opened in your default mail application.

2. MailHog

MailHog is another email testing tool that makes it easy to install and configure a local email server. MailHog sets up a fake SMTP server. You can configure your preferred web applications to use MailHog’s SMTP server to send and receive emails.

There are a couple of ways to install MailHog. For this article we’ll use Docker. Docker is one of the easiest ways to set up such a tool across any platform.

First, make sure that you have Docker installed on your machine. To start MailHog you just run the following command from your command line:

docker run --name=mailhog -p 2525:1025 -p 8025:8025 mailhog/mailhog

Let’s break down the above command and its directives:

Run your code which sends the email, open your browser and go to http://localhost:8025

mailhog email
MailHog with email

3. Papercut SMTP

Papercut SMTP is a 2-in-1 quick email viewer AND built-in SMTP server (designed to receive messages only). Papercut SMTP doesn’t enforce any restrictions how you prepare your email, but it allows you to view the whole email-chilada: body, html, headers, attachment down right down to the naughty raw encoded bits.

The quickest and simplest way to run and use Papercut SMTP is by using Docker. You can get started just by running this command from your command line:

docker run --name=papercut -p 2525:25 -p 37408:37408 jijiechen/papercut:latest

This will spin up a Papercut server locally in a Docker container. Papercut will listen on port 2525 and will host its web interface on port 37408 (feel free of course to change those ports to your needs).

When the container is spun up and the server is running, you can start sending your emails from your (web)application to on localhost. The web interface can be accessed from http://localhost:37408. Here you can see the received emails.

papercut email

4. Smtp4dev

Smtp4dev is a dummy SMTP server for Windows, Linux, Mac OS-X (and maybe elsewhere where .NET Core is available). Messages received in smtp4dev can be viewed and inspected via a sleek web-based GUI.

Smtp4dev can be setup either by installing it as a stand-alone application or service, or by spinning up a Docker container. Let’s create a docker container using the following command.

docker run --name=smtp4dev -p 2525:25 -p 3000:80 rnwood/smtp4dev

This will spin up a Smtp4dev server locally in a Docker container and have it listen on port 2525 and host its web interface on port 3000 (feel free of course to change those ports to your needs).

Run your code which sends the email, open your browser and go to http://localhost:3000

smtp4dev inbox
smtp4dev with email

 

Summary

As you have seen. It is not difficult to set up a local SMTP server for development purposes. By using Docker, you can spin up a local server in minutes without much effort.

Fake SMTP servers are a great tool when it comes to integration or functional testing. You don’t want to send real emails but you want to make sure that “real” behavior is happening at the same time. Personally, I’ve been using fakeSMTP for years in different projects without any problems.

Azzeddine Daddah

Read all my blogs

Receive our weekly blog by email?
Subscribe here: