This is a Spring Boot-based demo project for sending emails. It uses MailHog as an SMTP server for local testing and is fully Dockerized for easy execution.
1. Features
-
User Registration: Through a REST controller, users can register and receive a welcome email.
-
Sending Emails: Using JavaMailSender to send emails through MailHog in the development environment.
2. Requirements
-
Java 17 or higher.
-
Docker to run MailHog and the containerized application.
3. Technologies and Dependencies
-
Spring Boot: Core framework for building Java applications.
-
Spring Boot Starter Web: For creating RESTful web applications.
-
Spring Boot Starter Mail: For handling emails.
-
Lombok: A library to reduce boilerplate in code.
-
MailHog: SMTP server for local email testing.
-
Docker: For creating and managing application containers.
4. Configuration and Execution
-
If any of the following steps don't work, watch this video
-
Repository Clone
# Clone the repository
git clone https://github.com/youruser/email-notifications.git
cd email-notifications
- Before building the Docker image, you need to generate the JAR file of the project. Run the following command from the root directory of the project:
./mvnw clean package
# or
mvn clean package # (If you have Maven installed globally)
- This will create a .jar file inside the target/ directory, with a name similar to:
target/email-notifications-0.0.1-SNAPSHOT.jar
-
This file will be used by Docker to build the application image.
-
Before building and running the containers, make sure you have Docker running (for Windows, use Docker Desktop)
-
Once installed, make sure Docker is running
docker --version
- Once Docker is running, you can build and deploy the containers
docker-compose up --build
- If there is any problem building our service environment with Docker and if any image is in use and you cannot remove it directly, you can force the removal with the following command:
docker rmi -f $(docker images -q)
- Then, to do a general cleanup of everything unused (stopped containers, untagged images, unused networks, etc.), you can use the following command:
docker system prune -a --volumes
- We rebuild our containers
docker-compose up --build
5. Project Structure
emails-notifications-MailHog/
├── src/
│ ├── main/
│ │ ├── java/com/example/demo/
│ │ │ ├── controller/UserController.java
│ │ │ ├── model/User.java
│ │ │ └── service/EmailService.java
│ │ └── resources/
│ │ ├── application.properties
│ │ └── static/img/
│ │ ├── arg-flag.jpg
│ │ ├── eeuu-flag.jpg
│ │ └── mailhog.jpeg
│ │ └── static/translation/README.es.md
│ └── test/java/com/example/demo/EmailNotificationsApplicationTests.java
├── docker-compose.yml
├── Dockerfile
├── pom.xml
└── README.md
6. Processing Flow
-
User Registration: Receives a POST request with user data (name, email).
-
Email Sending: Sends a welcome email to the registered user using MailHog as SMTP.
-
Email Verification: The email can be viewed in the MailHog web interface.
7. Testing
- Verify Docker containers:
docker psYou should see two containers running:
springboot-appon port 8080mailhogon port 8025
- Verify application logs:
docker logs springboot-app-
The MailHog web interface will be available at
http://localhost:8025 -
Here you can see all emails sent by the application
-
The interface shows: sender, recipient, subject and email content
Endpoint: POST http://localhost:8080/api/users
Headers: Content-Type: application/json
Body:
{
"name": "John Doe",
"email": "john.doe@example.com"
}Expected response:
{
"status": 200,
"message": "User registered and email sent."
}Body:
{
"name": "Jane",
"email": "jane@test.com"
}Body:
{
"name": "María José",
"email": "maria.jose@company.com"
}After each successful registration, verify at http://localhost:8025:
-
Sender:
spring-boot@localhost -
Recipient: The email provided in the request
-
Subject:
Welcome [name] -
Content:
Hello [name], welcome to our platform!
# Basic registration
curl -X POST http://localhost:8080/api/users \
-H "Content-Type: application/json" \
-d '{"name": "Carlos López", "email": "carlos@example.com"}'-
Create new collection "Email Notifications"
-
Create POST request to
http://localhost:8080/api/users -
Add in Headers:
Content-Type: application/json -
Add in Body (raw JSON):
{
"name": "Test User",
"email": "test@example.com"
}-
Access
http://localhost:8025 -
Click "Delete All" to clear all emails
# Stop services
docker-compose down
# Restart services
docker-compose up --build
# Restart only the application
docker-compose restart app-
Verify that MailHog is running:
docker ps -
Verify MailHog logs:
docker logs mailhog -
Verify SMTP configuration in
application.properties
-
Verify that the application is running:
docker ps -
Verify application logs:
docker logs springboot-app -
Verify that port 8080 is available
# Clean Docker completely
docker system prune -a --volumes
docker-compose up --buildImportant: Now the application is Dockerized. We can stop or run the app directly with Docker.
8. Implemented Validations
-
Email must be present and valid format (handled by JavaMailSender, but you can extend validation in the User model or controller).
-
Name must be present.
9. Reports
-
MailHog Web Interface: All sent emails can be viewed at http://localhost:8025
-
Application Logs:
- View logs with
docker logs springboot-app - View MailHog logs with
docker logs mailhog
- View logs with
-
Troubleshooting: See the Troubleshooting section in Testing.
10. Contributing
-
Fork the project
-
Create your feature branch (
git checkout -b feature/AmazingFeature) -
Commit your changes (
git commit -m 'Add some AmazingFeature') -
Push to the branch (
git push origin feature/AmazingFeature) -
Open a Pull Request
11. License
This project is under the MIT License - see the LICENSE file for details.
12. About
This project implements a simple email notification system using Spring Boot and MailHog for local SMTP testing. It is fully Dockerized for easy local development and testing.
