Send email with Spring Boot (SMTP integration)

        This article explores Spring Boot integration with SMTP and how to send email from your own Spring Boot application.

        This article explores how to send emails from your own Spring Boot application.
        Yes, you can have a dedicated REST API that accepts the email address, subject and body of the email sender and recipient - the classic attributes that make up business email. Your front-end team can then call this API at will by passing the necessary parameters, and voila! Your email was sent with ease.
        Spring Boot provides built-in dependencies that contain all required methods. This can be used to send emails to valid email addresses. It's completely free and very easy to integrate with classic Spring Boot applications.
        In this article, we will see how to send emails from a simple Spring Boot application.
        Spring implements it using SimpleMailMessage and by integrating spring-boot-starter-mail.
        So, let's get started.

step 1

        Go to start.spring.io and create a new Spring Boot project with only the following dependencies

  • Spring web
  • Java mail sender

        Click Build Project. This will create a zip file, unzip it and open it in IntelliJ.
        At this point, you have a basic Spring Boot project. Just hit the play button to start the app and make sure everything is working.

 

Step 2: Configure our email server

        Next, we need to provide the configuration for Spring Mail. Just add the following property for email configuration to application.propertiesthe file:

# email configs

spring.mail.host=smtp.gmail.com

spring.mail.username=<your email id>

spring.mail.password=<your password>

spring.mail.properties.mail.smtp.auth=true

spring.mail.properties.mail.smtp.starttls.enable=true

step 3

Once all the required configurations are done, we can expose our rest endpoint/mail, which will accept incoming requests to send emails to a specific email address.

Let's add the following code for the controller layer to be able to access our API, which will trigger the email.

@RestController
public class EmailController {

    @Autowired
    private EmailService emailService;

    @PostMapping("/mail")
    public void sendEMail(@RequestBody EmailRequest emailRequest) {
        System.out.println("Going to Send email: " + emailRequest.toString());
        emailService.sendEmail(emailRequest);
    }
}

As we can see, the controller expects a request object namedEmailRequest.

Therefore, create a request body for email requests with the following properties to identify the email:

public class EmailRequest {

    // Class data members
    private String recipient;
    private String msgBody;
    private String subject;
    private String attachment;
    
    //generate getters 
}

service layer

Let's talk about the implementation class. It will contain an object that JavaMailSenderhas a method called send()with the following signature:

void send(SimpleMailMessage simpleMessage) throws MailException;

This class SimpleMailMessagecomes from package   "org.springframework.mail".

It has properties such as sender, recipient, text and subject. We set all these important properties based on the incoming request. As shown below, the send method will eventually be 'javaMailSender'triggered with .

@Service
public class EmailServiceImpl implements EmailService {

    @Autowired
    private JavaMailSender javaMailSender;

    @Override
    public void sendEmail(EmailRequest emailRequest) {

        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setFrom("abc@xyz");
        mailMessage.setTo(emailRequest.getRecipient());
        mailMessage.setText(emailRequest.getMsgBody());
        mailMessage.setSubject(emailRequest.getSubject());
        javaMailSender.send(mailMessage);
    }
}

At this point, we are done! Launch the app and head to Postman. We use spring's default port (8080), so calling the APIlocalhost:8080/mail

Call the API from Postman as follows:

 

in conclusion

In this article, we have learned an easy way to send emails from a Spring Boot application. It uses JMS JavaMailSenderto send emails through a simple REST API. 

Guess you like

Origin blog.csdn.net/qq_28245905/article/details/132202745