Skip to content

Mirian97/email-microservice-spring-boot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

12 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿ“ง Email Microservice with Spring Boot and AWS SES

Build Status Java Spring Boot AWS SES

Email Banner

This project is a Spring Boot-based microservice designed to send emails using Amazon Simple Email Service (SES) ๐Ÿš€. It provides a RESTful API to trigger email sending through HTTP requests, following a clean architecture pattern with clear separation of concerns. The microservice is lightweight, scalable, and perfect for integration in a microservices ecosystem ๐ŸŒ.

โœจ Features

  • Send emails using Amazon SES with customizable recipient, subject, and body ๐Ÿ“ฉ.
  • RESTful API endpoint to trigger email sending via POST requests ๐ŸŒ.
  • Clean architecture with separation of concerns (use cases, application, adapters, infrastructure) ๐Ÿงฉ.
  • Error handling for email sending failures ๐Ÿšจ.
  • Configurable email source address via environment properties โš™๏ธ.
  • Integration with AWS SES for reliable email delivery โ˜๏ธ.

๐Ÿ› ๏ธ Technologies Used

  • Java โ˜•: Programming language (version 11 or later).
  • Spring Boot ๐ŸŒฑ: Framework for building the microservice.
  • Amazon SES โ˜๏ธ: Email sending service via AWS SDK.
  • Maven ๐Ÿ“ฆ: Dependency management and build tool.
  • Spring Web ๐Ÿ•ธ๏ธ: For creating RESTful endpoints.
  • Spring Core ๐Ÿ”ฉ: For dependency injection and configuration.
  • AWS SDK for Java ๐Ÿ› ๏ธ: To interact with Amazon SES.

๐Ÿ“‹ Prerequisites

Before running the project, ensure you have:

  • Java Development Kit (JDK) 11 or higher โ˜•.
  • Maven 3.6 or higher ๐Ÿ“ฆ.
  • An AWS account with access to Amazon SES โ˜๏ธ.
  • AWS credentials (Access Key ID and Secret Access Key) with SES permissions ๐Ÿ”‘.
  • Git (optional, for cloning the repository) ๐Ÿ™.

โš™๏ธ Setup and Installation

  1. Clone the Repository:

    git clone https://github.com/Mirian97/email-microservice-spring-boot.git
    cd email-microservice-spring-boot
  2. Install Dependencies:

    ./mvnw clean install
  3. Configure AWS SES:

    • Set up your AWS SES account and move out of sandbox mode for production (or use sandbox for testing) โ˜๏ธ.
    • Obtain your AWS Access Key ID and Secret Access Key from the AWS IAM console ๐Ÿ”‘.
    • Verify an email address in SES to use as the emailSource ๐Ÿ“ง.
  4. Update Configuration: Configure the application.properties file (see ๐Ÿ”ง Configuration section).

  5. Run the Application:

    ./mvnw spring-boot:run

    The app starts on http://localhost:8080 by default.

๐Ÿ”ง Configuration

Update src/main/resources/application.properties or application.yml with your AWS credentials and email source. Example:

emailSource=verified-email@example.com

aws.accessKeyId=your-aws-access-key-id
aws.secretKey=your-aws-secret-key
aws.region=us-your-aws-region

springdoc.api-docs.path=/swagger-ui.html
springdoc.swagger-ui.enabled=true
springdoc.api-docs.path=/api-docs

For security, use environment variables or AWS Secrets Manager in production ๐Ÿ”.

If your your project is already running, you could check an online documentation with Swagger OpenApi, in this link http://localhost:8080/swagger-ui/index.html

๐ŸŒ API Endpoints

POST /api/email

Sends an email with the provided details ๐Ÿ“ฉ.

Request Body

JSON format:

{
	"to": "string",
	"subject": "string",
	"body": "string"
}
  • to: Recipient's email address ๐Ÿ“จ.
  • subject: Email subject line โœ‰๏ธ.
  • body: Email body content (plain text) ๐Ÿ“.

Response

  • Success โœ…: 200 OK
    "Email sent successfully"
  • Error โŒ: 500 INTERNAL SERVER ERROR
    "Error when sending email: <error-message>"

Example Request

curl -X POST http://localhost:8080/api/email \
-H "Content-Type: application/json" \
-d '{
  "to": "recipient@example.com",
  "subject": "Test Email",
  "body": "This is a test email sent via AWS SES."
}'

๐Ÿš€ Usage

  1. Start the application as described in โš™๏ธ Setup and Installation.
  2. Send a POST request to /api/email using curl, Postman, or any HTTP client ๐Ÿ“ฌ.
  3. Verify the response to confirm email sending โœ….
  4. Check the recipient's inbox (if verified in SES) or SES console for delivery status ๐Ÿ“Š.

๐Ÿšจ Error Handling

  • EmailServiceException: Handles email sending failures (e.g., invalid addresses, SES errors) โš ๏ธ.
  • AmazonServiceException: Wrapped in EmailServiceException for AWS-specific issues (e.g., authentication, limits) โ˜๏ธ.
  • Returns 500 INTERNAL SERVER ERROR with a descriptive message on failure โŒ.

๐Ÿ›๏ธ Architecture

The project follows a clean architecture pattern:

  • Core ๐Ÿง :
    • EmailSenderUseCase: Defines email sending contract.
    • EmailRequest: Record for email data (to, subject, body).
    • EmailServiceException: Custom exception for errors.
  • Application โš™๏ธ:
    • EmailSenderService: Implements EmailSenderUseCase, delegates to gateway.
  • Adapter ๐Ÿ”Œ:
    • EmailSenderGateway: Interface for email sending implementation.
  • Infrastructure ๐Ÿ—๏ธ:
    • SesEmailSender: Uses AWS SES to send emails.
    • AwsSesConfig: Configures SES client with credentials and region.
  • Controller ๐ŸŽฎ:
    • EmailSenderController: Handles HTTP requests and delegates to service.

This ensures modularity, testability, and flexibility to swap email providers.

About

๐Ÿ“ง A Spring Boot microservice for sending emails via Amazon SES, applying clean architecture.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages