Spring Boot provides convenient support for sending email messages using the JavaMailSender interface.
Need to include the spring-boot-starter-mail dependency. This starter module simplifies the configuration and usage of email functionality in Spring Boot application.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
Configure email properties:
emailconfig.recipients=<To_EMAIL_ID>
emailconfig.sender=<FROM_EMAIL_ID>
emailconfig.body=<EMAIL_BODY>
emailconfig.subject=<EMAIL_SUBJECT>
spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=<MAIL_USERNAME>
spring.mail.password=<MAIL_PASSWORD>
spring.mail.properties.mail.smtp.starttls.required=true
Create model to load the email configurations. The @ConfigurationProperties annotation is used to bind external configuration properties to a Java class. This annotation allows to map properties defined in configuration files (e.g., application.properties or application.yml) to fields in a POJO (Plain Old Java Object) class. It’s a convenient way to centralize and manage configuration properties in Spring Boot application.
package com.example.SpringEmailDemo.model;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Configuration
@ConfigurationProperties("emailconfig")
public class EmailConfig {
private String recipients;
private String sender;
private String body;
private String subject;
}
Create a service that will handle sending emails which uses JavaMailSender interface for this purpose.
package com.example.SpringEmailDemo.service.impl;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import com.example.SpringEmailDemo.model.EmailConfig;
import com.example.SpringEmailDemo.service.EmailService;
import jakarta.mail.internet.MimeMessage;
@Service
public class EmailServiceImpl implements EmailService{
@Autowired
private JavaMailSender javaMailSender;
public void sendEmail(EmailConfig emailConfig) {
MimeMessage mineMessage = javaMailSender.createMimeMessage();
MimeMessageHelper mimeMessageHelper;
try {
mimeMessageHelper = new MimeMessageHelper(mineMessage,true);
mimeMessageHelper.setFrom(emailConfig.getSender());
mimeMessageHelper.setTo(emailConfig.getRecipients());
mimeMessageHelper.setText(emailConfig.getBody());
mimeMessageHelper.setSubject(emailConfig.getSubject());
String fileName="D://1.txt";
FileSystemResource file = new FileSystemResource(new File(fileName));
Path path = Paths.get(fileName);
byte[] contents = Files.readAllBytes(path);
mimeMessageHelper.addAttachment(file.getFilename(), new ByteArrayResource(contents));
javaMailSender.send(mineMessage);
}
catch(Exception ex) {
ex.printStackTrace();
}
}
}
Controller to define API to send email.
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.SpringEmailDemo.model.EmailConfig;
import com.example.SpringEmailDemo.service.EmailService;
@RestController
public class SpringEmailDemoController {
@Autowired
EmailConfig emailConfig;
@Autowired
EmailService emailService;
@PostMapping("/sendEmail")
public void sendEmail() {
emailService.sendEmail(emailConfig);
}
}
For complete source code: https://github.com/MMahendravarman/Springboot_Examples
In this blog using the Spring WebFlux module, we are going to leverage the functional…
Spring Cloud Function is a project within the Spring ecosystem that allows developers to build…
RabbitMQ is an open-source message broker software that implements the Advanced Message Queuing Protocol (AMQP).…
Spring Integration is a powerful extension of the Spring Framework designed to support the implementation…
The Spring Cloud Config Client is a component of the Spring Cloud framework that enables…
In Python, handling CSV (Comma Separated Values) files is easy using the built-in csv module.…