Send Emails with SendGrid from the Micronaut Framework
Learn how to send emails with SendGrid from a Micronaut application.
On this guide
In this section
Getting Started
In this guide, we will create a Micronaut application written in Java.
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-sendgrid,validation,graalvm \
--build=gradle \
--lang=java \
--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-sendgrid, 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.
SendGrid
SendGrid is a transactional email service.
SendGrid is responsible for sending billions of emails for some of the best and brightest companies in the world.
SendGrid API Key
To use the Micronaut SendGrid integration, you need an API key.
You need to configure the sendgrid.api-key property. However, don’t put it in application.properties. It’s a password. Expose it via the SENDGRID_API_KEY environment variable, use a secret manager, or create an environment-specific configuration file that will not be included in source version control.
Dependency
Because we added the email-sendgrid feature, the application contains the following dependency:
implementation("com.micronaut.email:micronaut-email-sendgrid")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
API Key
Set the SendGrid API key as an environment variable before you run the application:
export SENDGRID_API_KEY=xxxFrom email address
Change the property micronaut.email.from.email to match your SendGrid configuration.
Run
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). |