HomeSpringSpringBootEasily Learn how to send an email with the Springboot email application...

Easily Learn how to send an email with the Springboot email application with 9 steps

Learn how to send an email with easy steps

Sending an email is a standard need for any app. In this post, you can learn how to send e-mails using the Springboot email application. You need SMTP information to send an email to work on this tutorial. You should use Gmail SMTP for the sample submission (keep in mind that Gmail has any restriction on the email quota). The Spring System offers a simple abstraction for sending emails using the JavaMailSender GUI, and Spring Boot provides auto-configuration for it and a starter module.

Maven Dependencies for Springboot email application

In order to allow Spring Boot auto-configuration for our program, we need to add a spring-boot-starter-mail to our pom.xml. This is the snapshot of the pom.xml:

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-mail</artifactId>
</dependency>

Mail Configurations Springboot email application

If the dependencies have been established, the next step is to add the configuration of the mail to the Spring Boot Auto Configuration:

spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username= your gmail id
spring.mail.password= your gmail password
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true

The following configuration values for Gmail SMTP are replaced by your SMTP configurations:

Writing Email Service Springboot email application

Develop a basic email service class to deliver emails to our server. Our sample email service will include two methods:

  1. Send out simple emails
  2. Emails with attachment

This is how our code look like:

package com.onurdesk.email.service;

import java.io.FileNotFoundException;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;

@Service
public class DefaultEmailService implements EmailService {


    @Autowired
    public JavaMailSender emailSender;

    @Override
    public void sendSimpleEmail(String toAddress, String subject, String message) {

        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setTo(toAddress);
        simpleMailMessage.setSubject(subject);
        simpleMailMessage.setText(message);
        emailSender.send(simpleMailMessage);
    }

    @Override
    public void sendEmailWithAttachment(String toAddress, String subject, String message, String attachment) throws MessagingException, FileNotFoundException {

        MimeMessage mimeMessage = emailSender.createMimeMessage();
        MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true);
        messageHelper.setTo(toAddress);
        messageHelper.setSubject(subject);
        messageHelper.setText(message);
        FileSystemResource file= new FileSystemResource(ResourceUtils.getFile(attachment));
        messageHelper.addAttachment(" Order Purchase", file);
        emailSender.send(mimeMessage);
    }
}

Now Testing Controller Springboot email application

Let’s set up a basic REST controller to verify our job. Our REST controller will have two ways. This is how our controller looks like:

package com.onurdesk.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.mail.MailException;
import org.springframework.web.bind.annotation.*;

import com.onurdesk.email.service.EmailService;

import javax.mail.MessagingException;
import java.io.FileNotFoundException;

@RestController
@RequestMapping("/email")
public class EmailController {

    private static final Logger LOG = LoggerFactory.getLogger(EmailController.class);

    @Autowired
    EmailService emailService;

    @GetMapping(value = "/simple-email/{user-email}")
    public @ResponseBody
    ResponseEntity<String> sendSimpleEmail(@PathVariable("user-email") String email) {

        try {
            emailService.sendSimpleEmail(email, "Welcome", "This is a welcome email for your!!");
        } catch (MailException mailException) {
            LOG.error("Error while sending out email..{}", mailException.getStackTrace());
            LOG.error("Error while sending out email..{}", mailException.fillInStackTrace());
            return new ResponseEntity<>("Unable to send email", HttpStatus.INTERNAL_SERVER_ERROR);
        }

        return new ResponseEntity<>("Please check your inbox", HttpStatus.OK);
    }


    @GetMapping(value = "/simple-order-email/{user-email}")
    public @ResponseBody
    ResponseEntity<String> sendEmailAttachment(@PathVariable("user-email") String email) {

        try {
            emailService.sendEmailWithAttachment(email, "Order Confirmation", "Thanks for your recent order", "classpath:purchase_order.pdf");
        } catch (MessagingException | FileNotFoundException mailException) {
            LOG.error("Error while sending out email..{}", mailException.getStackTrace());
            LOG.error("Error while sending out email..{}", mailException.fillInStackTrace());
            return new ResponseEntity<>("Unable to send email", HttpStatus.INTERNAL_SERVER_ERROR);
        }

        return new ResponseEntity<>("Please check your inbox for order confirmation", HttpStatus.OK);
    }

}

Now it’s time for the Springboot main class

package com.onurdesk;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SendEmailUsingSpringApplication {

	public static void main(String[] args) {
		SpringApplication.run(SendEmailUsingSpringApplication.class, args);
	}

}

Now configure Gmail SMTP

If you are using Gmail SMTP, please refer to the URL below for information. Your usual email password would not function due to additional encryption in Gmail.

How to Send Email using Spring Boot and Thymeleaf

In the obvious portion, we’ve seen measures to send emails using Spring, but for a production application, we need a more efficient way to send emails. In this segment, we’ll look at how to send emails using Spring Boot and Thyleaf.

Thyleleaf is a template generator. Thymeleaf will help us send rich HTML emails to you. Suppose we want to give an email to the client for proof of account verification.

EmailContext Class Springboot email application

To make it more flexible, we will create EmailContext class which will holds all the core information we need for email. This is how our class look like:

public abstract class AbstractEmailContext {

    private String from;
    private String to;
    private String subject;
    private String email;
    private String attachment;
    private String fromDisplayName;
    private String emailLanguage;
    private String displayName;
    private String templateLocation;
    private Map <String, Object> context;
    //getter and seter methods
}

Let ‘s talk about a couple of fields in this class:

templateLocation – Determines the location of our HTML template. We’re going to use this to produce the final output.

context – This includes all the complex values that have been transferred in the email. Thymeleaf uses this map to substitute expressions with real values (e.g. the first name, etc.).

Next, we’ll change our email service to process the HTML templates of Thymeleaf to create email material.

EmailService for Themleaf engine

package com.onurdesk.core.email.service;

    import com.onurdesk.core.email.context.AbstractEmailContext;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.mail.javamail.JavaMailSender;
    import org.springframework.mail.javamail.MimeMessageHelper;
    import org.springframework.stereotype.Service;
    import org.thymeleaf.context.Context;
    import org.thymeleaf.spring5.SpringTemplateEngine;
    
    import javax.mail.MessagingException;
    import javax.mail.internet.MimeMessage;
    import java.nio.charset.StandardCharsets;
    
    @Service
    public class DefaultEmailService implements EmailService {
    
        @Autowired
        private JavaMailSender emailSender;
    
        @Autowired
        private SpringTemplateEngine templateEngine;
    
        @Override
        public void sendMail(AbstractEmailContext email) throws MessagingException {
            MimeMessage message = emailSender.createMimeMessage();
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(message,
                MimeMessageHelper.MULTIPART_MODE_MIXED_RELATED,
                StandardCharsets.UTF_8.name());
            Context context = new Context();
            context.setVariables(email.getContext());
            String emailContent = templateEngine.process(email.getTemplateLocation(), context);
    
            mimeMessageHelper.setTo(email.getTo());
            mimeMessageHelper.setSubject(email.getSubject());
            mimeMessageHelper.setFrom(email.getFrom());
            mimeMessageHelper.setText(emailContent, true);
            emailSender.send(message);
        }
    }

There are some significant things happening in the code above.

  • We’re building and setting the map as variables. Thymeleaf uses this meaning to replace the placeholder or expressions with the values passed through the context.
  • SpringTemplateEngine is an abstraction of a different template engine which can select a configured template engine. It’s Thymeleaf, in our situation.
  • The templateEngine.process) (will process and output as a string. This will select the HTML template, process it, and overwrite the expressions with the actual values.
  • Finally, we set the content in the MimeMessage and request the mail server to handle the content as HTML (mimeMessageHelper.setText(emailContent, true))

Email Template

Here’s our email HTML prototype (I’ve taken some HTML to save some space).

<!DOCTYPE html>
    <html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office">
    <head>
        <meta charset="utf-8"> <!-- utf-8 works for most cases -->
        <meta name="viewport" content="width=device-width"> <!-- Forcing initial-scale shouldn't be necessary -->
        <meta http-equiv="X-UA-Compatible" content="IE=edge"> <!-- Use the latest (edge) version of IE rendering engine -->
        <meta name="x-apple-disable-message-reformatting">  <!-- Disable auto-scale in iOS 10 Mail entirely -->
        <title></title> <!-- The title tag shows in email notifications, like Android 4.4. -->
    
        <link href="https://fonts.googleapis.com/css?family=Lato:300,400,700" rel="stylesheet">
    
        <!-- CSS Reset : BEGIN -->
     </head>
    
    <body width="100%" style="margin: 0; padding: 0 !important; mso-line-height-rule: exactly; background-color: #f1f1f1;">
    <center style="width: 100%; background-color: #f1f1f1;">
        <div style="display: none; font-size: 1px;max-height: 0px; max-width: 0px; opacity: 0; overflow: hidden; mso-hide: all; font-family: sans-serif;">
            &zwnj;&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;
        </div>
        <div style="max-width: 600px; margin: 0 auto;" class="email-container">
            <!-- BEGIN BODY -->
            <table align="center" role="presentation" cellspacing="0" cellpadding="0" border="0" width="100%" style="margin: auto;">
                <tr>
                    <td valign="top" class="bg_white" style="padding: 1em 2.5em 0 2.5em;">
                        <table role="presentation" border="0" cellpadding="0" cellspacing="0" width="100%">
                            <tr>
                                <td class="logo" style="text-align: center;">
                                    <h1>Welcome</h1>
                                </td>
                            </tr>
                        </table>
                    </td>
                </tr><!-- end tr -->
                <tr>
                    <td valign="middle" class="hero bg_white" style="padding: 2em 0 4em 0;">
                        <table>
                            <tr>
                                <td>
                                    <div class="text" style="padding: 0 2.5em; text-align: center;">
                                        <h2>Finish creating your account.</h2>
                                        <h3>Hi<p th:text="${firstName}"></h3>
                                        <h3>We're excited to have you get started. First, you need to confirm your account. Just press the button below.</h3>
                                        <p><a th:href="${verificationURL}" class="btn btn-primary">Validate Account</a></p>
                                    </div>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <div class="text" style="padding: 0 2.5em; text-align: center;">
                                        <h3>if you experience any issues with the button above, copy and paste the URL below into your web browser.</h3>
                                        <p th:text="${verificationURL}"></p>
                                    </div>
                                </td>
                            </tr>
                        </table>
                    </td>
                </tr><!-- end tr -->
                <!-- 1 Column Text + Button : END -->
            </table>
            <table align="center" role="presentation" cellspacing="0" cellpadding="0" border="0" width="100%" style="margin: auto;">
                <tr>
                    <td valign="middle" class="bg_light footer email-section">
                        <table>
                            <tr>
                                <td valign="top" width="33.333%" style="padding-top: 20px;">
                                    <table role="presentation" cellspacing="0" cellpadding="0" border="0" width="100%">
                                        <tr>
                                            <td style="text-align: left; padding-right: 10px;">
                                                <h3 class="heading">About</h3>
                                                <p>Welcome to onurdesk. We publish articles on
                                                    Spring, Spring Boot and Spring Security.
                                                </p>
                                            </td>
                                        </tr>
                                    </table>
                                </td>
                                <td valign="top" width="33.333%" style="padding-top: 20px;">
                                    <table role="presentation" cellspacing="0" cellpadding="0" border="0" width="100%">
                                        <tr>
                                            <td style="text-align: left; padding-left: 5px; padding-right: 5px;">
                                                <h3 class="heading">Contact Info</h3>
                                                <ul>
                                                    <li><span class="text">Java Development Journal</span></li>
                                                </ul>
                                            </td>
                                        </tr>
                                    </table>
                                </td>
                                <td valign="top" width="33.333%" style="padding-top: 20px;">
                                    <table role="presentation" cellspacing="0" cellpadding="0" border="0" width="100%">
                                        <tr>
                                            <td style="text-align: left; padding-left: 10px;">
                                                <h3 class="heading">Useful Links</h3>
                                                <ul>
                                                    <li><a href="#">Home</a></li>
                                                    <li><a href="#">About</a></li>
                                                </ul>
                                            </td>
                                        </tr>
                                    </table>
                                </td>
                            </tr>
                        </table>
                    </td>
                </tr><!-- end: tr -->
            </table>
    
        </div>
    </center>
    </body>
    </html>

Please keep in mind that we need to set the meaning for ${firstName} and ${verificationURL} in the background such that Thymeleaf will insert the real value for these variables throughout processing.

Admin
Onurdesk is a technical platform based primarily on Spring, Java, and Nodejs ecosystem app creation. We write tutorials and manuals concentrating extensively on realistic and real-life usage cases.

Most Popular

Recent Comments