Send Emails with Amazon SES from the Micronaut Framework
Learn how to send emails with Amazon SES from a Micronaut application.
On this guide
In this section
Getting Started
In this guide, we will create a Micronaut application written in Kotlin.
What you will need
To complete this guide, you will need the following:
-
Some time on your hands
-
A decent text editor or IDE (e.g. IntelliJ IDEA)
-
JDK 21 or greater installed with
JAVA_HOMEconfigured appropriately
Solution
We recommend that you follow the instructions in the next sections and create the application step by step. However, you can go right to the completed example.
-
Download and unzip the source
Writing the Application
Create an application using the Micronaut Command Line Interface or with Micronaut Launch.
mn create-app example.micronaut.micronautguide \
--features=email-amazon-ses,aws-v2-sdk,validation,graalvm \
--build=gradle \
--lang=kotlin \
--test=junit|
Note
|
If you don’t specify the --build argument, Gradle with the Kotlin DSL is used as the build tool. If you don’t specify the --lang argument, Java is used as the language.If you don’t specify the --test argument, JUnit is used for Java and Kotlin, and Spock is used for Groovy.
|
The previous command creates a Micronaut application with the default package example.micronaut in a directory named micronautguide.
If you use Micronaut Launch, select Micronaut Application as application type and add email-amazon-ses, aws-v2-sdk, validation, and graalvm features.
|
Note
|
If you have an existing Micronaut application and want to add the functionality described here, you can view the dependency and configuration changes from the specified features, and apply those changes to your application. |
Controller
Create a MailController class. This class uses a collaborator, emailSender, to send an email.
You can send emails asynchronously using the AsyncEmailSender API or synchronously using the EmailSender API.
Configuration
If you want to send every email with the same address, you can set it via configuration:
micronaut.email.from.email=john@micronaut.exampleThis is possible thanks to email decorators.
AWS SES
Amazon Simple Email Service (Amazon SES) is a cloud-based email-sending service designed to help digital marketers and application developers send marketing, notification, and transactional emails. It is a reliable, cost-effective service for businesses of all sizes that use email to keep in contact with their customers.
Dependency
Because we added the email-amazon-ses feature, the application contains the following dependency:
implementation("com.micronaut.email:micronaut-email-amazon-ses")Micronaut AWS SDK v2
Micronaut Amazon SES integration uses Micronaut AWS SDK v2 integration.
Because we added the aws-v2-sdk feature, the application contains the following dependency:
implementation("io.micronaut.aws:micronaut-aws-sdk-v2")Read about:
Test
Create a test bean that replaces the bean of type AsyncTransactionalEmailSender.
Write a test that uses EmailSenderReplacement to verify that the contents of the email match expectations.
Testing the Application
To run the tests:
./gradlew testThen open build/reports/tests/test/index.html in a browser to see the results.
Running the Application
From email address
Change the property micronaut.email.from.email to match your Amazon SES verified sender.
Supply AWS Credentials
An easy way to supply AWS Credentials is to define the following environment variables:
export AWS_ACCESS_KEY_ID=xxx
export AWS_SECRET_ACCESS_KEY=xxxRun
To run the application, use the ./gradlew run command, which starts the application on port 8080.
Invoke
curl -d '{"to":"john@micronaut.example"}' \
-H "Content-Type: application/json" \
-X POST http://localhost:8080/mail/sendNext Steps
Explore more features with Micronaut Guides.
Learn more about Micronaut Email integration.
Help with the Micronaut Framework
The Micronaut Foundation sponsored the creation of this Guide. A variety of consulting and support services are available.
License
|
Note
|
All guides are released with an Apache License 2.0 for the code and a Creative Commons Attribution 4.0 license for the writing and media (images). |