Getting Started with Spring Email Services

It’s time to add Email Services in our NixMash Spring app, so here we’ll cover our initial mail configuration in Spring. We’ll create a new module in IntelliJ (or a new project in Eclipse) called “mail.” Here we see its familiar Spring Structure.

to do

Our “mail” application is a Gradle Child Project of our NixMash-Spring Parent project so we have little to do in our build.gradle file other than add the Spring Boot Mail Starter dependency.

A primary piece of our Mail app is our MailSender component which extends JavaMailSenderImpl and is where we set our Hostname, Port and other server properties.

Notice we’re using a MailSettings component for our properties because we’re going to retrieve them from an external properties file for the most flexibility when moving between environments.

Now we move to our MailService implementation class where we put our pieces into play using our mailSender component to send a populated Mail DTO object.

We’ll call the service from a MailUI component to keep things simple. As you can deduce, the first use of our new email services will be with a Web Contact Form, so for now we’ll hard code the form elements.

Let’s run it.

Excellent. Email for everybody!

Source Code Notes for this Post

All source code discussed in this post can be found in my NixMash Spring GitHub repo and viewed online here.