11.6: Goal
- Send email notification when the contact form is submitted
- Use Spring Boot Mail (SMTP)
- Secure credentials using .env (Docker + VPS)
- Production-ready setup (no hardcoding secrets)
This is exactly the right stage to introduce email as a side-effect service in our backend. Since our stack is already production-oriented (Docker, CI/CD, Redis, PostgreSQL), we’ll implement this cleanly with separation of concerns, async handling, and secure config.
We’ll implement this in 7 structured steps:
1️⃣ Choose SMTP Provider (Critical Decision)
Use a reliable SMTP provider—do NOT use raw Gmail in production unless it's temporary.
Recommended:
- SendGrid (best balance)
- Mailgun
- Amazon SES (best for scale)
👉 For now, I’ll assume Gmail SMTP (quick start), but the structure will be provider-agnostic.
2️⃣ Add Dependency
pom.xml
3️⃣ Environment Variables (SECURE)
On VPS → .env
⚠️ Use App Password, NOT your Gmail password.
Docker Compose (important)
services:
contact-api:
env_file:
- .env
4️⃣ Spring Boot Config (NO HARDCODING)
application-prod.yml
5️⃣ Create Email Service Layer
📁 Structure
service/
EmailService.java
impl/
EmailServiceImpl.java
6️⃣ Integrate with Contact Flow
- 👉 DO NOT send email directly in controller
- 👉 Use service layer after DB save
7️⃣ Make It Production-Grade (IMPORTANT)
✅ A. Async Email (Non-blocking API)
Add:
Main class
Update Email Service
👉 Prevents API delay if SMTP is slow
✅ B. Add Logging + Failure Handling
✅ C. Optional (Advanced - Recommended Later)
- Retry using Spring Retry
- Queue-based system (Redis / Kafka)
- HTML emails (Thymeleaf templates)
🔥 Architecture After This
Controller
↓
ContactService
↓
PostgreSQL (save)
↓
EmailService (async)
↓
SMTP Provider
🚀 Deployment Checklist
- .env added on VPS
- Docker container restarted
- SMTP credentials verified
- Port 587 allowed (some VPS block it ⚠️)
- Test via API (Postman/frontend)
11.6: Goal
This is exactly the right stage to introduce email as a side-effect service in our backend. Since our stack is already production-oriented (Docker, CI/CD, Redis, PostgreSQL), we’ll implement this cleanly with separation of concerns, async handling, and secure config.We’ll implement this in 7 structured steps:
1️⃣ Choose SMTP Provider (Critical Decision)
Use a reliable SMTP provider—do NOT use raw Gmail in production unless it's temporary.
Recommended:
👉 For now, I’ll assume Gmail SMTP (quick start), but the structure will be provider-agnostic.
2️⃣ Add Dependency
3️⃣ Environment Variables (SECURE)
4️⃣ Spring Boot Config (NO HARDCODING)
5️⃣ Create Email Service Layer
📁 Structure
6️⃣ Integrate with Contact Flow
7️⃣ Make It Production-Grade (IMPORTANT)
✅ A. Async Email (Non-blocking API)
✅ B. Add Logging + Failure Handling
✅ C. Optional (Advanced - Recommended Later)
🔥 Architecture After This
🚀 Deployment Checklist