How to send emails in Spring Boot?

To send emails using the Spring Boot Email Starter in a Spring Boot application, you can do the following:

STEP 1: Add the Spring Boot Email Starter dependency to your project:

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

STEP 2: Configure the connection to the email server in your application.properties file:

spring.mail.host=your-mail-server
spring.mail.port=25
spring.mail.username=your-username
spring.mail.password=your-password

STEP 3 Inject the JavaMailSender bean into your service and use it to send emails:

@Service
public class MailService {

  @Autowired  private final JavaMailSender mailSender;


  public void sendEmail(String to, String subject, String body) {
    SimpleMailMessage message = new SimpleMailMessage();
    message.setTo(to);
    message.setSubject(subject);
    message.setText(body);
    mailSender.send(message);
  }

}

This is just a basic example of how to send emails using the Spring Boot Email Starter and Spring Boot.

You can also send attachments along with your email.

For this you should use MimeMessage instead of SimpleMailMessage in the above example.

Here is an example:

@Service
public class MailService {

  @Autowired
  private JavaMailSender mailSender;


  public void sendEmailWithAttachments(String to, String subject, String body, List<File> attachments) throws MessagingException {
    MimeMessage message = mailSender.createMimeMessage();

    MimeMessageHelper helper = new MimeMessageHelper(message, true);
    helper.setTo(to);
    helper.setSubject(subject);
    helper.setText(body);

    for (File attachment : attachments) {
      helper.addAttachment(attachment.getName(), attachment);
    }

    mailSender.send(message);
 }

Advertisements

Using Javax Mail API:

If you don’t want to use the spring boot starter dependency , you can also use Javax Mail API and achieve the same.

Here is an example of how you can use the Java Mail API to send an email in a Spring Boot application:

STEP 1: Add the Java Mail dependency to your project:

<dependency>
  <groupId>com.sun.mail</groupId>
  <artifactId>javax.mail</artifactId>
  <version>1.6.2</version>
</dependency>

STEP 2 : Create a MailSender bean to configure the connection to the email server:

@Configurationpublic class MailConfig {

  @Value("${mail.host}")
  private String host;

  @Value("${mail.port}")
  private int port;

  @Value("${mail.username}")
  private String username;

  @Value("${mail.password}")
  private String password;

  @Bean
  public JavaMailSender getJavaMailSender() {
    JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
    mailSender.setHost(host);
    mailSender.setPort(port);

    mailSender.setUsername(username);
    mailSender.setPassword(password);

    Properties props = mailSender.getJavaMailProperties();
    props.put("mail.transport.protocol", "smtp");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.debug", "true");

    return mailSender;
  }

}

STEP 3: Inject the MailSender bean into your service and use it to send emails:

@Service
public class MailService {

  @Autowired  private JavaMailSender mailSender;


  public void sendEmail(String to, String subject, String body) {
    SimpleMailMessage message = new SimpleMailMessage();
    message.setTo(to);
    message.setSubject(subject);
    message.setText(body);
    mailSender.send(message);
  }

}

That’s it!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s