Updated March 30, 2023
Introduction to Spring boot email
In our application, we may encounter a requirement where we have to send emails based on some conditions, for this spring boot provide us with an easy way to send them without making any changes to the existing code, we have to add the dependency provided by the spring boot framework at our classpath. This provides us lots of interface and classes that can be used to prepare the email and send to the user whenever required. We can send the email using the Gmail SMTP server, and they can send it by using the Transport layer security of Gmail. We have to make more configurations rather than just adding the dependency to the build file; in the coming section of the tutorial, we will see how we can configure this in our application and the internal working and implementation for better understating and clarity.
Syntax
As we have discussed, this is used to send the email to the user whenever it is required; for this, we have to make changes in the application property file; let’s take a closer look at the syntax for better understanding; see below;
spring.mail.host= host name
spring.mail.port= port number
spring.mail.username= user for smtp server
spring.mail.password= password for smtp server
spring.mail.properties.mail.smtp.auth= true or false
spring.mail.properties.mail.smtp.starttls.enable= true or false
As you can see in the above lines of syntax, we have to make these configurations inside the application file in order to read them and use them while sending the email to the user. In the coming section of the tutorial, we will have a closer look at each configuration in detail for better understanding for beginners.
How does Spring boot email work?
As we have already known, it enables the developers to send the email; this email can be sent in any of the requirements. We have some basic scenarios where every application needs this when we have any failure in the application of any exception or error that occurred in the application. In such cases, we can send emails to the developer in order to let them know about the error, with the specified logs and error message. Also, use as the notifier for the developer. In short, it can be used as the notifier service as well, apart from the usage for it depends on the requirement, because in the application we can have various requirements where we have to send email to user like, on successful registration, password reset and many more.
Let’s take a closer look at the different types of classes and interface available for this; see below;
1) MailSender Interface: It is a top-level interface that helps and provide us with the basic functionality by which we can send simple emails from our application
2) JavaMailSender Interface: This interface extends the MailSender, interface hence the sub-interface for it. It is basically used to prepare the MIMEMessage by the conjunction of the MimeMessageHelper class; It is very recommended to use one more interface, which is named MimeMessagePreparator.
3) JavaMailSenderImpl class: It is a class that helps us implement the JavaMailSender interface. Basically, it implements this interface; it supports SimpleMailMessage and MimeMessage.
4) SimpleMailMessage class: This is also a class that is used to create the simple message for mails such as text fields, cc, to, from ad subject.
5) MimeMessagePreparator interface: It is an interface that provides us with a callback interface by which we can prepare the MIME message.
6) MimeMessageHelper class: This class is a helper class that helps us to create the MIME message; it also provides us support for the image text content in HTML and mail attachments, etc.
Also, we add one dependency to enable ad use of the library for email; for reference, please see below;
e.g. :
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
Examples of Spring boot email
Follow the below steps to configure email in the spring boot application, a simple application to send the emails by using the JavaSender, by giving the implementation to it.
a) Create a project by using spring initializer and provide all the details there, for reference find the URL below,
URL: https://start.spring.io/
b) Now, we have to add one more dependency in order to use the library for creating the email in spring boot; for reference, see below;
e.g.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
c) Now, in the application file, we have to put the detail in order to use this while sending the emails; for reference, see the below code; motioned you user and password for the SMTP account to make this work.
e.g. :
spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=your SMTP user
spring.mail.password=your SMTP password
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
d) Paste the below code in the main class; this will give us the bean for JavaSender at the time of creation; just copy the code; nothing more is needed:
e.g. :
@SpringBootApplication
public class TradersApplication {
public static void main(String[] args) {
SpringApplication.run(TradersApplication.class, args);
}
@Bean
public Javasender getJavasender() {
JavasenderImpl sender = new JavasenderImpl();
sender.setHost("smtp.gmail.com");
sender.setPort(587);
sender.setUsername("your gamil acc");
sender.setPassword("your pass");
Properties pop = sender.getJavaMailProperties();
pop.put("mail.transport.protocol", "smtp");
pop.put("mail.smtp.auth", "true");
pop.put("mail.smtp.starttls.enable", "true");
pop.put("mail.debug", "true");
return sender;
}
}
e) create one simple service to send the email using Javasender bean we need to autowired the bean here in order to use it, for reference see below code;
e.g. :
@Component
public class EmailServiceDemoImpl implements EmailService {
@Autowired
private JavaMailSender sender;
public void sendEmail(String to, String subject, String text) {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom("[email protected]");
message.setTo(to);
message.setSubject(subject);
message.setText(text);
sender.send(message);
System.out.pritln("Message sent successfully");
}
}
Output:
Conclusion
As we have seen how to enable the email in the spring boot application, adding one dependency and few lines of configuration is an easy way for developers to create the custom email in the application itself and send them using the Gmail transport layer security. We just have to specify the details of the SMTP users inside the application file in order to send. This is easy to learn, readable, and maintain as well.
Recommended Articles
This is a guide to Spring Boot Email. Here we discuss the working of Spring Boot Email along with an example and its implementation for better understating and clarity. You may also have a look at the following articles to learn more –