recursive.codes

recursive.codes

recursive.codes


The Personal Blog of Todd Sharp

Sending Email With OCI Email Delivery From Micronaut

Posted By: Todd Sharp on 3/18/2022 8:00 GMT
Tagged: Java, Micronaut

Email delivery is a critical function of most web applications in the world today. I've managed an email server in the past - and trust me - it's not fun or easy to do for a developer that isn't as familiar with the infrastructure side of things. Thankfully, Oracle Cloud Infrastructure (OCI) offers an email delivery service that provides a managed solution for sending high-volume emails without setting up and managing an SMTP server. My favorite Java framework, Micronaut, recently released the Micronaut Email module that supports sending email in your applications. This release inspired me to see what it would take to use OCI email delivery in a Micronaut application. As usual, it's pretty straightforward. So let's see what it takes!

Setup OCI Email Delivery

If you've not yet configured a user for email delivery, you'll need to take care of that first. Normally, I'd walk you through that process, but in this case, I think the docs do an excellent job of keeping things concise, so I'll link to them here instead. Here are the steps you'll need to take:

We'll need to collect our username, password, and SMTP endpoint before moving on to the next step.

Sending Email via OCI Email Delivery in Your Micronaut App

Now that we have the appropriate credentials and setup complete, we can move on to adding the Micronaut module to our application and configuring it to get ready to send emails.

Add Dependencies

We'll need the following dependencies to enable our Micronaut application to send emails. If you don't want to use templates, you could exclude the last two dependencies, but I'm going to include them since I'll be showing how to use templates.

 Tip! You can choose any of the template engines that are supported by Micronaut Views for email templates!

Configure Default Sender

We can specify a default sender for all emails by setting one into our configuration at /src/main/resources/application.yml:

Note! The default email sender must be an "approved sender" in the OCI email delivery service!

Configure MailPropertiesProvider

Next, we will need to configure a MailPropertiesProvider. Creating the provider is accomplished via configuration settings in our application.yml file (located at /src/main/resources). The necessary configuration to generate the provider is below. Note that we'll set the host value externally in just a bit.

Configure SessionProvider

A SessionProvider is also necessary. For this, we'll create a class that implements SessionProvider and within the session() method, we'll create and return an instance of PasswordAuthentication. We will pass the username and password via the constructor. Again, we'll set those values via external configuration in the next section of this blog post.

Set Credentials Into the Application via Environment Variables

You could hardcode your credentials into your configuration files, but that would be a bad idea. It's so bad that I don't even do that for simple local demos anymore. We must insist on consistently enforcing security practices in our applications. So, if we're not hardcoding them, how can we get them into the app? Well, one way is to set them into Java system properties. Another option is to set them into environment variables. Micronaut can translate a specifically constructed environment variable or system property into the proper configuration value. If you're not familiar with this feature, I suggest that you read about it in the Application Configuration section of the docs. So, to pass in the credentials, I set the following environment variables in my IDE:


Send Email with EmailSender

At this point, we're ready to inject the Micronaut EmailSender into our application and use it to send email messages. I've created a controller to test this out and inject the EmailSender like so:

Send a Simple Plain Text Email

To send an email, use the Email builder:

To test this, hit the /email endpoint:

Checking the inbox where we sent the message will confirm the message delivery.

Send a Templated Email

To send an email using a Thymeleaf template, create a template at /src/main/resources/views/email.html and populate it like so:

Create an endpoint:

Send an email by requesting the /email/template endpoint and pass the value for /{name} like so: 

And check the inbox:

Send an Email With an Attachment

To send a message with an attachment, create a POST endpoint located at /email/attachment that consumes a multipart form.

And POST a request to the /email/attachment endpoint that includes a file:

Check the inbox:

Summary

This post looked at how to use OCI email delivery in a Micronaut application to send text-based and templated email messages. We also learned how to send emails with attachments with the EmailSender. As always, you can refer to the project on GitHub for the full source code used in this demo.



Related Posts

Querying Autonomous Database from an Oracle Function (The Quick, Easy & Completely Secure Way)

Querying Autonomous Database from an Oracle Function (The Quick, Easy & Completely Secure Way)

I've written many blog posts about connecting to an Autonomous DB instance in the past. Best practices evolve as tools, services, and frameworks become...

Brain to the Cloud - Part III - Examining the Relationship Between Brain Activity and Video Game Performance

Brain to the Cloud - Part III - Examining the Relationship Between Brain Activity and Video Game Performance

In my last post, we looked at the technical aspects of my Brain to the Cloud project including much of the code that was used to collect and analyze the...

Brain to the Cloud - Part II - How I Uploaded My Brain to the Cloud

Brain to the Cloud - Part II - How I Uploaded My Brain to the Cloud

In my last post, we went over the inspiration, objectives, and architecture for my Brain to the Cloud project. In this post, we'll look in-depth at the...

Note: Comments are currently closed on this blog. Disqus is simply too bloated to justify its use with the low volume of comments on this blog. Please visit my contact page if you have something to say!