- Quarkus guide: https://quarkus.io/guides/mailer
This is a minimal application sending emails with Gmail. It follows the instructions from the getting started guide, but also contains some extra examples:
-
You need to generate an application password to use GMail from the application. Follow these instructions to create the password.
-
You need Java 11+.
-
You need GraalVM and
native-image
installed and configured. Follows the instructions to download, install, and configure GraalVM.
Launch the Maven build on the checked-out sources of this demo:
> ./mvnw package
Generate the native executable using:
> ./mvnw package -Dnative
The application contains tests that used the mock mailer to avoid sending actual emails during the tests. The dev mode also uses this mock mailer.
The application uses GMAIL. Define the following environment properties:
QUARKUS_MAILER_USERNAME
- your Google email addressQUARKUS_MAILER_FROM
- your Google email address (or alias)QUARKUS_MAILER_PASSWORD
- the application password generated above
Run the application with:
> java -jar ./target/quarkus-app/quarkus-run.jar
Thanks to the environment properties defined above, the application should authenticate with Gmail and send the email.
To send the email, use the /mail
endpoint.
Before doing so:
- Change the email address used in
src/main/java/org/acme/MailResource.java
, so you can receive the email - If you want to send an email (and not just simulate it), add
quarkus.mailer.mock=false
to thesrc/main/resources/application.properties
file
Then, in a terminal use
curl -v :8080/mail
You can also create a native executable from this application without making any source code changes. A native executable removes the dependency on the JVM: everything needed to run the application on the target platform is included in the executable, allowing the application to run with minimal resource overhead.
Compiling a native executable takes a bit longer, as GraalVM performs additional
steps to remove unnecessary code paths. Use the native
profile to compile a
native executable:
> ./mvnw package -Dnative
After getting a cup of coffee, you'll be able to run this executable directly:
> ./target/mailer-quickstart-1.0.0-SNAPSHOT-runner
Then send an email using the same curl command.
Before running the other demos, don't forget to edit the code to use your email address.
- Code: using attachments
- Command:
curl -v http://localhost:8080/extra/attachment
- Code: using template
- Template: template
- Command:
curl -v http://localhost:8080/extra/template
- Code: using type safe template
- Template: template
- Command:
curl -v http://localhost:8080/type-safe