How to send mail in spring boot

In Spring Boot, you can send mail using the JavaMailSender interface. Here's a step-by-step guide:

1. Add dependencies

In your pom.xml file (if you're using Maven) or build.gradle file (if you're using Gradle), add the following dependencies:

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

<!-- Gradle -->
dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-mail'
}

2. Configure mail settings

Create a application.properties file (or application.yml if you prefer YAML) and add the following settings:

spring:
  mail:
    host: your-mail-host
    port: 587
    username: your-mail-username
    password: your-mail-password
    protocol: smtp
    default-encoding: UTF-8

Replace your-mail-host, your-mail-username, and your-mail-password with your actual mail server settings.

3. Create a JavaMailSender instance

In your Spring Boot application, create a JavaMailSender instance:

import org.springframework.boot.autoconfigure.mail.MailProperties;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;

@Configuration
public class MailConfig {
    @Bean
    public JavaMailSender javaMailSender() {
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
        mailSender.setHost(MailProperties.getHost());
        mailSender.setPort(MailProperties.getPort());
        mailSender.setUsername(MailProperties.getUsername());
        mailSender.setPassword(MailProperties.getPassword());
        return mailSender;
    }
}

4. Send mail

Now you can use the JavaMailSender instance to send mail:

import org.springframework.mail.javamail.MimeMessagePreprocessor;
import org.springframework.mail.javamail.MimeMessagePreprocessorAdapter;
import org.springframework.mail.javamail.MimeMessagePreprocessorChain;
import org.springframework.mail.javamail.MimeMessagePreprocessorChainAdapter;

@Service
public class MailService {
    @Autowired
    private JavaMailSender javaMailSender;

    public void sendMail(String to, String subject, String body) {
        MimeMessagePreprocessorChain chain = new MimeMessagePreprocessorChainAdapter();
        MimeMessagePreprocessor preprocessor = new MimeMessagePreprocessorAdapter(chain);
        MimeMessagePreprocessorChainAdapter adapter = new MimeMessagePreprocessorChainAdapter(chain);

        MimeMessage message = javaMailSender.createMimeMessage();
        message.setSubject(subject);
        message.setText(body);
        message.setFrom("your-email-from");
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));

        javaMailSender.send(message);
    }
}

In this example, we create a MimeMessage instance and set the subject, body, from address, and recipient. We then use the JavaMailSender instance to send the mail.

5. Test

Create a test class to test the mail sending functionality:

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class MailServiceTest {
    @Autowired
    private MailService mailService;

    @Test
    public void testSendMail() {
        mailService.sendMail("[email protected]", "Test Subject", "Test Body");
    }
}

Run the test class to verify that the mail is sent successfully.

That's it! You've successfully set up mail sending in your Spring Boot application.