Mailing for Spring

Spring provides mail started dependency that makes working with Jakarta Mail easier.

The main advantage Spring abstraction provides is that we don’t have to deal with jakarta Session in any way.

Additionally, Spring also provides helper classes to make constructing email messages easier.

By including dependency in your project:

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

and defining spring.mail.* properties for host, port, username and password:

spring.mail.host=localhost
spring.mail.port=1025
spring.mail.username=user123
spring.mail.password=admin

Spring autoconfigures and exposes JavaMailSender bean which we can inject and use to send mail.

If we need to send simple text messages, we can use SimpleMailMessage and for html messages and for messages using inline images and attachments we can MimeMessage (multipart message).