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.
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
Option B: Go
go install github.com/mailhog/MailHog@latest
Option C: Docker (Recommended)
docker run -d -p 1025:1025 -p 8025:8025 mailhog/mailhog
2. Run MailHog
If you're using Docker, it's already running:
- SMTP Server β
localhost:1025
- Web UI β http://localhost:8025
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
Optional: Make it toggleable using environment variables:
n1netails:
email:
enabled: ${EMAIL_ENABLED:false}
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:
- Email templates are stored in the DB
- I load them via a Spring Data repository
- Dynamic content is injected using
{{placeholders}}
- Emails are sent using
JavaMailSender
Templates are pre-loaded using Liquibase.
π₯ SQL Table for Templates
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)
);
π¨ HTML Welcome & Alert Email Templates
- Welcome & Alert Template SQL
- Includes
{{username}}
,{{n1netailsEmail}}
, and more
𧬠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)