Groovy / Gradle

Send Emails with SendGrid from the Micronaut Framework

Learn how to send emails with SendGrid from a Micronaut application.

Sergio del Amo
On this guide
In this section

Getting Started

In this guide, we will create a Micronaut application written in Groovy.

What you will need

To complete this guide, you will need the following:

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.

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 \
    --build=gradle \
    --lang=groovy \
    --test=spock
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, and validation 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.

groovy/src/main/groovy/example/micronaut/MailController.groovy

Configuration

If you want to send every email with the same address, you can set it via configuration:

src/main/resources/application.properties
micronaut.email.from.email=john@micronaut.example

This 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:

build.gradle
implementation("com.micronaut.email:micronaut-email-sendgrid")

Test

Create a test bean that replaces the bean of type AsyncTransactionalEmailSender.

groovy/src/test/groovy/example/micronaut/EmailSenderReplacement.groovy

Write a test that uses EmailSenderReplacement to verify that the contents of the email match expectations.

groovy/src/test/groovy/example/micronaut/MailControllerSpec.groovy

Testing the Application

To run the tests:

./gradlew test

Then 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=xxx

From 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/send

Next 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).