preface

In real projects, email notification is often needed. For example, users register via email, retrieve the password via email, etc. For example, sending system information through email, sending report information through email and so on, there are many practical application scenarios. In this article, we will teach you to quickly implement a function of sending emails through Springboot.

Open SMTP

Here take QQ mailbox as an example. After logging in to QQ mailbox, click Settings, click account. The diagram below:

Click to enable IMAP/SMTP service.

Write down the authorization code prompted by QQ mailbox:

This authorization code is the password needed to send the email.

Once the above steps are complete, you can begin development.

Introduction of depend on

In the SpringBoot project, introduce the following dependencies:

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

The configuration file

After introducing dependencies in a SpringBoot project, you can configure the mailbox parameters in its configuration file:

application.properties

spring.mail.host=smtp.qq.com
[email protected]
spring.mail.password=xxxxxxxx
spring.mail.protocol=smtp
spring.mail.default-encoding=UTF-8
Copy the code

Because it is a QQ mailbox, the host needs to use smtp.qq.com. If it’s another email address, do a search.

Username is the email account, and password is the authorization code mentioned in the preceding steps.

Inject the JavaMailSender instance

Once the mailbox parameters are configured, you can inject the JavaMailSender instance directly.

@Autowired
private JavaMailSender javaMailSender;
Copy the code

Send regular mail

Regular mail, no style. It is easiest to implement:

@Test
public void testSend(a) throws MessagingException {
    SimpleMailMessage message = new SimpleMailMessage();
    message.setFrom("[email protected]");
    message.setTo("[email protected]");
    message.setSubject("Here's the title.");
    message.setText("This is the content.");
    javaMailSender.send(message);
}
Copy the code

The value of From must be the same as that of username in the configuration file; otherwise, an error message is displayed.

To is the email recipient;

Subject is the Subject of the email;

Text indicates the content of the email.

Run the method to receive mail:

Send HTML mail

Most of the time, we need the email to have a nice look. At this point, you can use the HTML style. We need to build a MimeMessage using the createMimeMessage method of the javaMailSender, and then create the MimeMessageHelper using the MimeMessage instance. As follows:

@Test
public void testSend(a) throws MessagingException {
    MimeMessage mimeMessage = javaMailSender.createMimeMessage();
    MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage);
    messageHelper.setSubject("Title");
    messageHelper.setFrom("[email protected]");
    messageHelper.setTo("[email protected]");
    messageHelper.setText("

Title



This is the content

"
.true); javaMailSender.send(messageHelper.getMimeMessage()); } Copy the code

Note that you need to pass a Boolean value to your setText table and use HTML for the table name.

Run the method to view the result:

As you can see, the h1 style is already out.

conclusion

This article explains how to send emails quickly in SpringBoot. There are two ways to send emails: 1. 2. HTML mode. In fact, it also supports a variety of rich patterns, such as template engines, etc., which I will not explain here, but in fact, the use of similar.