Quote of the Day

more Quotes

Categories

Buy me a coffee

  • Home>
  • Java>

Easily test sending and receiving email locally using MailHog

Published September 23, 2022 in Java , Testing - 0 Comments

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: 0.0.0.0:1025
[HTTP] Binding to address: 0.0.0.0:8025
2022/09/21 21:28:49 Serving under http://0.0.0.0:8025/
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

    @Override
    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);
            props.put("mail.smtp.host", SMTP_HOST);
            Session session = Session.getDefaultInstance(props, null);
            session.setDebug(false);

            Message msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress(SENDER, SENDER_ALIAS));
            msg.setReplyTo(InternetAddress.parse(replyTo, false));
            msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recepient, false));
            msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc, false));

            msg.setSubject(subject);
            msg.setContent(content, "text/html");
            // -- Send the message --
            Transport.send(msg);
            LOGGER.debug("Email message was sent successfully!");

        } catch (UnsupportedEncodingException | MessagingException ex) {
            LOGGER.error("Error occured while trying to send email", ex);
        }
    }

In the above snippets, I hardcoded the smtp port and smtp host for demonstration purpose. In reality, we load the configs from the property file, based on the target environment.

Below screenshot shows how the trapped email coming from the application to MailHog. The web interface is reachable at localhost:8025 .

Trap emails for testing using MailHog

In conclusion, I find MailHog is super easy to use and just what I need for local testing. Although I have not explored MailHog further, it should be possible to incorporate MailHog for automated integration tests. For instance, you can have your application running on a container which has MailHog configured to run automatically. If you are looking for a way to test email related features, checkout MailHog.

Resources

Mailhog

Mailhog: How to Use it and Why

No comments yet