A robust, cloud-native email notification service built with Spring Boot, supporting a hybrid strategy of AWS Simple Email Service (SES) and Standard SMTP (Gmail, Outlook, etc.). This service provides a centralized API for sending emails, managing dynamic templates, and tracking delivery status with a resilient retry mechanism.
- Multi-Provider Strategy: Seamlessly switch between AWS SES and SMTP providers at runtime.
- Dynamic Strategy Factory: Smartly handles retries by maintaining the original provider for each specific email log.
- Centralized Email API: Unified interface for sending emails across multiple applications.
- Synchronous & Asynchronous Sending: Supports both immediate feedback and background processing.
- Dynamic Templating: Integration with Thymeleaf for rich HTML email templates with dynamic content.
- Resilient Retry Mechanism: Built-in background scheduler that retries transient failures with status tracking.
- PostgreSQL Persistence: Detailed logging of every email attempt, including provider tracking and error messages.
- Client Authentication: Secure access management using Client ID and Secret headers.
- Framework: Spring Boot 3.5.7
- Language: Java 21
- Email Providers:
- AWS SES (AWS SDK v2)
- Java SMTP (Support for Gmail, Outlook, etc.)
- Database: PostgreSQL
- Templating: Thymeleaf
- Build Tool: Maven
The application requires specific properties in application.properties to manage the active strategy and credentials:
| Property | Description | Default Value |
|---|---|---|
email.provider.active |
Currently active provider (AWS_SES or SMTP) |
AWS_SES |
aws.ses.sender-email |
Default verified sender for AWS SES | - |
spring.mail.host |
SMTP Host (e.g., smtp.gmail.com) |
- |
spring.mail.username |
SMTP Account Username | - |
spring.mail.password |
SMTP Password (use App Passwords for Gmail) | - |
email.retry.max-retries |
Max retry attempts for sending | 3 |
server.port |
Application Port | 8081 |
The service implements the Strategy Pattern to decouple the sender logic:
- Interface:
EmailSenderStrategydefines the contract. - Implementations:
AwsSesStrategyImplandSmtpStrategyImplhandle specific vendor logic. - Factory:
EmailStrategyFactoryresolves the correct engine at runtime based on the email's assigned provider.
| Method | Endpoint | Description |
|---|---|---|
POST |
/api/v1/email/send/sync |
Send email synchronously. |
POST |
/api/v1/email/send/async |
Send email asynchronously. |
GET |
/api/v1/email/status/{id} |
Get status of an email by ID. |
Example Request:
{
"to": ["recipient@example.com"],
"subject": "System Alert",
"body": "Your account has been accessed.",
"priority": "HIGH"
}-
Clone and Build:
mvn clean install
-
Database Setup: Ensure PostgreSQL is running and credentials are updated in
application.properties. -
Configure Strategy:
- Set
email.provider.activetoAWS_SESorSMTP. - Provide the corresponding credentials (AWS keys or SMTP login).
- Set
-
Run:
mvn spring-boot:run
Managed under the avk_core_services schema:
clients: API access management.email_templates: HTML template storage.emails: Central log of all emails, including theproviderused and currentstatus.