[JAVA] [Spring Boot] Send an email

Send mail using spring-boot-starter-mail. Spring Boot is written in 2.0.9.

Add dependency

Add a dependency for spring-boot-starter-mail.

pom.xml


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

Add settings

Set the following under spring.mail. With this setting, you can only send emails to Gmail / GSuite users (see below).

application.yml


spring:
  mail:
    host: aspmx.l.google.com
    port: 25

About the contents set here

This time it's for testing purposes, so I'm sending emails using Google's restricted Gmail SMTP server. With this setting, you can only send email to Gmail or GSuite users. See the documentation for more information on the Gmail SMTP server.

-Send emails from printers, scanners and apps -G Suite Administrator Help

Supplement

Details of mail related settings in Spring Boot can be found in the documentation # Email.

Added test send code

You can send an email at startup by adding the following.

import org.springframework.mail.MailException;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.stereotype.Component;

@Component
public class MailUtil {
    private final MailSender mailSender;

    public MailUtil(MailSender mailSender) {
        this.mailSender = mailSender;

        this.sendMail();
    }

    public void sendMail() {
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setFrom("[email protected]"); //Sender email address
        mailMessage.setTo(/*Destination email address*/);
        mailMessage.setCc(/*Email address to put in cc*/);
        mailMessage.setBcc(/*Email address to put in bcc*/);
        mailMessage.setSubject("Test title");
        mailMessage.setText("Test message to send from local");

        try {
            mailSender.send(mailMessage);
        } catch (MailException e) {
            // TODO:Error handling
        }
    }
}

Transmission result

You can receive emails as follows. スクリーンショット 2019-07-05 20.png

Recommended Posts

[Spring Boot] Send an email
Send email with spring boot
Create an app with Spring Boot 2
Create an app with Spring Boot
Send an email from gmail with Ruby
Send regular notifications with LineNotify + Spring Boot
[Java] Send an email using Amazon SES
Challenge Spring Boot
Send an email using JavaMail on AWS
Spring Boot Form
Spring Boot Memorandum
gae + spring boot
I want to send an email in Java.
An introduction to Spring Boot + in-memory data grid
SPRING BOOT learning record 01
Spring Boot + Heroku Postgres
Spring boot memo writing (1)
First Spring Boot (DI)
SPRING BOOT learning record 02
Spring Boot2 cheat sheet
Spring Boot exception handling
Spring Boot Servlet mapping
Spring boot development-development environment-
Spring Boot learning procedure
Learning Spring Boot [Beginning]
Spring Boot 2.2 Document Summary
[Spring Boot] DataSourceProperties $ DataSourceBeanCreationException
Spring Boot 2.3 Application Availability
Spring boot tutorials Topics
Download with Spring Boot
Load an external jar from a Spring Boot fat jar
[Spring Boot] Environment construction (macOS)
Steps required to issue an asynchronous event for Spring Boot
Set context-param in Spring Boot
Try Spring Boot from 0 to 100.
Generate barcode with Spring Boot
Hello World with Spring Boot
Spring Boot on Microsoft Azure
Implement GraphQL with Spring Boot
Spring Boot tutorial task schedule
Spring 5 & Spring Boot 2 Hands-on preparation procedure
Get started with Spring boot
Ruby: Send email with Starttls
Hello World with Spring Boot!
Spring Boot 2 multi-project in Gradle
[Spring Boot] Web application creation
spring boot port duplication problem
Run LIFF with Spring Boot
SNS login with Spring Boot
Spring Boot Hot Swapping settings
[Java] Thymeleaf Basic (Spring Boot)
Introduction to Spring Boot ① ~ DI ~
File upload with Spring Boot
Spring Boot starting with copy
Introduction to Spring Boot ② ~ AOP ~
CICS-Run Java application-(4) Spring Boot application
[2020 version] How to send an email using Android Studio Javamail
Spring Boot starting with Docker
Spring Boot + Springfox springfox-boot-starter 3.0.0 Use
Spring Boot DB related tips
Hello World with Spring Boot