Easily test sending and receiving email locally using MailHog

Tai Bo
3 min readSep 23, 2022

A java application which I work on has a feature to send emails via the corporate SMTP server to notify certain personnel whenever an error occurs. For security reasons, only servers within a certain networks can access the SMTP server and send emails. For instance, the computer which I use to build the application does not have access to the SMTP server. When searching for an email testing tool, I stumbled upon MailHog. Within minutes, I was able to run MailHog and test sending emails without having to deploy the app to the remote servers.

For my situation, all I need is a way to validate the content of the email which my app generates and sends out to the specific addresses. This process is a manual verification, and not of an automated one. For this purpose, MailHog works great and is also extremely easy to setup. I have tested it on a Windows machine with a 64 bit operating system.

MailHog is extremely easy to setup. In fact, I did not have to install anything. I just had to download the executable file for my platform. You can find the MailHog releases here. For me, I downloaded the MailHog_windows_amd64.exe file.

Running the file will open a command prompt windows which display outputs from the app, as shown in the snippets below.

2022/09/21 21:28:49 Using in-memory storage
2022/09/21 21:28:49 [SMTP] Binding to address:
[HTTP] Binding to address:
2022/09/21 21:28:49 Serving under
Creating API v1 with WebPath:
Creating API v2 with WebPath:

Now, it’s ready for using. Note that I need to leave the MailHog instance running while testing. From my java app, I can send emails to the MailHog instance via SMTP port 1025, as shown in the above snippets.

Below shows the relevant snippets from my java applications for sending emails

public void sendMail(String replyTo, String recepient, String cc, String subject, String content) {
try {
Properties props = System.getProperties();
if (cc == null) {
cc = "";

final String SENDER = "myapp@email.com";
final String SENDER_ALIAS = "My web app";

// MailHog SMTP config for local testing.
final String SMTP_PORT = 1025;
final String SMTP_HOST = "localhost";

props.put("mail.smtp.port", SMTP_PORT)…
Tai Bo

Backend developer in .NET core. I enjoy the outdoor, hanging out with good friends, reading and personal development.