DEV Community

Cover image for πŸ“¨ Mocking Emails in a Spring Boot App Using MailHog – How I Integrated Email Testing into N1netails
Shahid Foy
Shahid Foy

Posted on

πŸ“¨ Mocking Emails in a Spring Boot App Using MailHog – How I Integrated Email Testing into N1netails

Recently, I added a new email notification feature to my N1netails project β€” a developer-focused alerting tool built with Spring Boot. One of the contributors suggested we look into using MailHog for local email testing, which turned out to be a great fit.

πŸ“¬ Now, new users receive a welcome email when signing up, and existing users get alerts via email when something important happens.

Welcome email screenshot

Alert email screenshot

When deploying to production via N1netails Dashboard, I registered a real email address via Outlook. But for local development and testing, I needed a way to simulate sending emails without actually sending them.

That’s where MailHog came in β€” a lightweight, fake SMTP server with a built-in web UI for viewing test emails.


πŸ”§ What is MailHog and Why Use It in a Spring Boot Project?

MailHog is a tool that captures outgoing emails sent from your app, so you can:

  • βœ… Test email templates without spamming real inboxes
  • βœ… Debug SMTP configurations locally
  • βœ… Preview emails in a browser

It’s ideal for Spring Boot developers building user signup flows, alerting systems, or any feature involving transactional emails.


πŸš€ Step-by-Step: How to Use MailHog with Spring Boot

1. Install MailHog

Option A: Binary Download

Download the binary from MailHog GitHub Releases

# Example for Windows:
mv MailHog_windows_amd64.exe mailhog.exe
Enter fullscreen mode Exit fullscreen mode

Option B: Go

go install github.com/mailhog/MailHog@latest
Enter fullscreen mode Exit fullscreen mode

Option C: Docker (Recommended)

docker run -d -p 1025:1025 -p 8025:8025 mailhog/mailhog
Enter fullscreen mode Exit fullscreen mode

2. Run MailHog

If you're using Docker, it's already running:


3. Configure Spring Boot to Use MailHog

Update your application.yml (or use Spring profiles):
View how it is configured in n1netails here application-email.yml

spring:
  mail:
    host: localhost
    port: 1025
    username: dummy
    password: dummy
    from: support@n1netails.com
    properties:
      mail.smtp.auth: false
      mail.smtp.starttls.enable: false
Enter fullscreen mode Exit fullscreen mode

Optional: Make it toggleable using environment variables:

n1netails:
  email:
    enabled: ${EMAIL_ENABLED:false}
Enter fullscreen mode Exit fullscreen mode

4. Trigger an Email

You can test by creating an account on the live N1netails Dashboard β€” or just simulate it in your code.


5. Preview Emails in the Web UI

Go to http://localhost:8025 to:

  • View email subject and content
  • Inspect headers
  • Test HTML rendering

πŸ› οΈ How N1netails Uses Email Templates (Spring Boot + Liquibase)

Here’s how I wired the backend email flow inside N1netails:

  1. Email templates are stored in the DB
  2. I load them via a Spring Data repository
  3. Dynamic content is injected using {{placeholders}}
  4. Emails are sent using JavaMailSender

Templates are pre-loaded using Liquibase.

πŸ“₯ SQL Table for Templates

db.changelog-00012:

CREATE TABLE IF NOT EXISTS ntail.email_notification_template
(
    id BIGINT NOT NULL,
    name character varying(100),
    subject TEXT,
    html_body TEXT,
    CONSTRAINT email_notification_template_pkey PRIMARY KEY (id)
);
Enter fullscreen mode Exit fullscreen mode

🎨 HTML Welcome & Alert Email Templates


🧬 Spring Boot Code for Sending Emails in N1netails

Entity:
EmailNotificationTemplateEntity.java

Repository:
EmailNotificationTemplateRepository.java

Service:
EmailServiceImpl.java

It fetches the email template by name, applies dynamic parameters, and sends using JavaMailSender.


βœ… Summary

With MailHog, I was able to:

  • Add robust email notifications to N1netails
  • Test locally with no risk of spamming users
  • Preview and iterate on email designs
  • Keep emails stored and maintainable in a database

If you're building a Spring Boot SaaS, consider adding MailHog to your local dev workflow!


πŸ’¬ If you found this useful or want to see more behind-the-scenes of N1netails, feel free to follow or ask questions. I'm building this open and dev-first. If you are interested in contributing you can view the GitHub

🦊 Happy coding!

Top comments (0)