DEV Community

Abraham Mekonnen
Abraham Mekonnen

Posted on

Sending Emails with Spring Boot, AWS SES, and Serverless Lambda for Scalable Solutions

In the course of developing a Next.js authentication project, I encountered the need to send verification emails. While there are many email-sending services available, most come with subscription fees or limited free-tier plans. To maintain control and reduce costs, I decided to build my own email sender using Spring Boot, which provides a powerful framework for Java-based backend development.

Initially, I used Gmail’s SMTP server to send emails. While functional, it lacked the professionalism of domain-specific emails, particularly for production environments. My goal was to send emails using my own domain, hosted on AWS Route 53. This led me to leverage AWS Simple Email Service (SES), which provides an SMTP interface for seamless integration.

In this article, I will outline my journey and provide an overview of the tools and resources I used to implement this solution.

Why AWS SES?

AWS SES stands out as the most cost-effective option, offering the ability to send up to 100,000 emails for just $10, compared to estimated costs of around $8 for 5,000 emails with Bravo or approximately $20 for 75,000 emails with SendGrid. Along with its scalability, reliability, and support for domain-specific emails, AWS SES becomes the ideal choice for transactional and marketing needs. Its superior deliverability rates and seamless integration with other AWS services further enhance its value, making it a robust and budget-friendly solution for businesses aiming to optimize their email campaigns.

Setting Up AWS SES

To use AWS SES, you need an AWS account. Setting up SES involves domain verification and creating an SMTP user. While I won’t cover the full step-by-step process here, I recommend the following YouTube tutorial, which I found incredibly helpful. Additionally, refer to the official AWS documentation for best practices and the most up-to-date setup instructions.

Moving forward his tutorial guided me through:

  1. Verifying my domain in AWS SES.
  2. Configuring DNS records in AWS Route 53 for domain authentication (DKIM and SPF).
  3. Setting up SES in production mode (moving out of the SES sandbox).

Integrating AWS SES with Spring Boot

After setting up AWS SES, the next step was to build an email sender in Spring Boot. For this, I referred to another excellent YouTube tutorial. However, additional documentation can be found here.

Key Steps in Implementation

1.Spring Boot Dependencies
Add the following dependencies to your pom.xml for email functionality:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
</dependency>
Enter fullscreen mode Exit fullscreen mode

2.Configuring SMTP Properties
In the application.properties or application.yml, configure the SMTP settings for AWS SES:

spring:
  application:
    name:
      MailSender
  mail:
    host: email-smtp.<region>.amazonaws.com
    port: 587
    username: ${mail-username}
    password: ${mail-password}
    protocol: smtp
    properties:
      mail:
        debug: true
        smtp:
          auth: true
          starttls:
            enable: true
            required: true

Enter fullscreen mode Exit fullscreen mode

3.Building the Email Service
To handle email sending, I created a service class. For email content, I utilized Mustache templates to design the email in HTML format, making it dynamic and visually appealing. The service simply injects the verification token into the template before sending the email.

Optimizing with AWS Lambda

After successfully implementing the email sender in Spring Boot, I decided to take it further by deploying the service as a Lambda function. Lambda provides serverless capabilities, reducing infrastructure overhead and costs.

Steps to Deploy with AWS Lambda

  1. Base Project I referred to the official AWS GitHub repository for serverless Java projects
  2. Deploying with AWS SAM Using the Serverless Application Model (SAM) CLI, I packaged and deployed the Lambda function. The setup included an API Gateway with API key-based authentication to secure the endpoint.
  3. Performance Enhancements While the initial deployment worked well, I sought to optimize the solution by:
  • Reducing memory consumption.
  • Improving cold start times for faster execution and lower costs.

Next Steps: GraalVM Native Image

To achieve even greater performance, my next goal is to use GraalVM Native Image. This technology compiles Java applications into native executables, eliminating the JVM’s startup time and significantly reducing memory usage. Stay tuned for my next article, where I’ll dive into this optimization and share how it further enhanced my email-sending service.

Conclusion

By integrating AWS SES with Spring Boot and deploying it using AWS Lambda, I built a scalable, cost-efficient email-sending service tailored to my domain-specific needs. This project not only met the immediate requirement of sending verification emails but also provided a learning opportunity in serverless architecture and performance optimization.

If you’re looking to implement a similar solution or have questions, feel free to reach out!

Top comments (0)