How to Send Emails with Spring Boot in Minutes: A Step‑by‑Step Guide

This guide explains the fundamentals of email protocols, shows how to configure Spring Boot’s mail starter, and provides complete code examples for sending plain‑text and HTML emails—including dependency setup, property configuration, and unit‑test snippets—so developers can quickly integrate reliable email functionality into their Java backend applications.

Senior Brother's Insights
Senior Brother's Insights
Senior Brother's Insights
How to Send Emails with Spring Boot in Minutes: A Step‑by‑Step Guide

Email Protocol Overview

SMTP

Simple Mail Transfer Protocol (SMTP) is the standard protocol for transmitting email between servers. It uses TCP port 25 and supports authentication to reduce spam.

POP3

Post Office Protocol version 3 (POP3) retrieves email from a server, typically deleting the messages after download. It operates on TCP port 110.

IMAP

Internet Message Access Protocol (IMAP) improves on POP3 by allowing selective download of messages and server‑side folder management. It uses TCP port 143.

Spring Boot Mail Integration

Dependency

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

Configuration

Add the following properties to application.properties. Adjust the host, username, and password for your email provider.

spring.mail.host=smtp.qq.com
[email protected]
spring.mail.password=123456
spring.mail.default-encoding=UTF-8

For other providers replace the host, e.g., smtp.163.com for 163.com.

Enabling IMAP/SMTP for QQ Mail

Log into QQ Mail, go to Settings → Account → POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV services, enable the IMAP/SMTP service, and copy the displayed authorization code. Use this code as spring.mail.password.

Enable IMAP/SMTP
Enable IMAP/SMTP

Sending Plain‑Text Email

Inject JavaMailSender and use SimpleMailMessage to send a basic email.

@RunWith(SpringRunner.class)
@SpringBootTest
public class MailTests {

    @Resource
    private JavaMailSender javaMailSender;

    @Test
    public void sendMail() {
        SimpleMailMessage msg = new SimpleMailMessage();
        msg.setFrom("[email protected]");
        msg.setTo("[email protected]");
        msg.setSubject("程序新视界");
        msg.setText("技术分享");
        // Optional CC:
        // msg.setCc("[email protected]", "[email protected]");
        javaMailSender.send(msg);
    }
}

Sending HTML Email

Use MimeMessage with MimeMessageHelper for rich‑text content, attachments, or inline resources.

@Test
public void sendHtmlMail() {
    String content = "<html>
" +
        "<body>
" +
        "    <h3>hello world ! 这是一封html邮件!</h3>
" +
        "</body>
" +
        "</html>";
    MimeMessage message = javaMailSender.createMimeMessage();
    try {
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom("[email protected]");
        helper.setTo("[email protected]");
        helper.setSubject("程序新视界");
        helper.setText(content, true);
        // Example attachment:
        // helper.addAttachment("file.txt", new File("path/to/file"));
        javaMailSender.send(message);
    } catch (MessagingException e) {
        System.out.println("发送邮件异常");
    }
}

Additional Considerations

In production environments you may need email templates, external email service APIs, comprehensive error handling, delivery monitoring, and attachment management.

Source code repository: https://github.com/secbr/springboot-learn/tree/master/springboot-mail

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendHTML emailSpring BootEmailSMTPJavaMailSender
Senior Brother's Insights
Written by

Senior Brother's Insights

A public account focused on workplace, career growth, team management, and self-improvement. The author is the writer of books including 'SpringBoot Technology Insider' and 'Drools 8 Rule Engine: Core Technology and Practice'.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.