In the development of the project, in addition to the need for SMS verification, sometimes email is also used to save SMS fees. Sending mail in a Spring project requires an inconvenient encapsulation of a complex message body. Sending emails in a Spring Boot project is too easy. Let’s take a look at how Spring Boot sends emails.

This article takes mailbox 126 as an example to send mails. The configurations of other mailboxes are similar with minor differences.

1. Obtain the authorization code

Commonly used electronic protocols are POP3, SMTP, IMAP, protocol specific differences will not be introduced in detail. SMTP is selected for demonstration. Log in to the email, find the protocol address in the Settings, and click Open. The authorization code is displayed only once and must be saved.

The following are the three protocol host addresses corresponding to mailbox 126:

2. Add dependencies

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

3. Configure the mailbox information

Note that the password is not the email login password, but the authorization code obtained in the first step.

spring:
  mail:
    default-encoding: utf-8Host address: SMTP126.Com # email address username: XXX @126.Com # Authorization code (not password) password: XXXXXXXXXXCopy the code

4. Send emails

Encapsulate the SimpleMailMessage message content, inject JavaMailSender and call its send() method to finish sending the message. Among them, the recipient and cc support multiple sending, multiple addresses, splicing together to complete batch sending.

@RestController
public class Email {

    @Autowired
    private JavaMailSender mailSender;
    @GetMapping("send")
    private void send(a){

        SimpleMailMessage message = new SimpleMailMessage();
        / / the sender
        message.setFrom("[email protected]");
        / / the recipient
        message.setTo("[email protected]");
        // The subject of the email
        message.setSubject("Java sends mail second bullet");
        // The content of the email
        message.setText("Hello, this is a message to test the Spring Boot mail sending function! Ha ha ha ~ ~ ~");
        / / cc
        message.setCc("[email protected]"); mailSender.send(message); }}Copy the code

5. Send effect

Finally, let’s see if any of the three mailboxes mentioned above received any data.

The sender:

The recipient:

Cc:


The sample code for this article has been uploaded togithub, point astarSupport!

Spring Boot series tutorial directory

Spring-boot-route (I) Several ways for Controller to receive parameters

Spring-boot-route (2) Several methods of reading configuration files

Spring-boot-route (3) Upload multiple files

Spring-boot-route (4) Global exception processing

Spring-boot-route (5) Integrate Swagger to generate interface documents

Spring-boot-route (6) Integrate JApiDocs to generate interface documents

Spring-boot-route (7) Integrate jdbcTemplate operation database

Spring-boot-route (8) Integrating mybatis operation database

Spring-boot-route (9) Integrate JPA operation database

Spring-boot-route (10) Switching between multiple data sources

Spring-boot-route (11) Encrypting database configuration information

Spring-boot-route (12) Integrate REDis as cache

Spring-boot-route RabbitMQ

Spring-boot-route Kafka

Spring-boot-route (15) Integrate RocketMQ

Spring-boot-route (16) Use logback to produce log files

Spring-boot-route (17) Use AOP to log operations

Spring-boot-route (18) Spring-boot-adtuator monitoring applications

Spring-boot-route (19) Spring-boot-admin Monitoring service

Spring-boot-route (20) Spring Task Implements simple scheduled tasks

Spring-boot-route (21) Quartz Implements dynamic scheduled tasks

Spring-boot-route (22) Enables email sending

Spring-boot-route (23) Developed wechat official accounts

Spring-boot-route (24) Distributed session consistency processing

Spring-boot-route (25) two lines of code to achieve internationalization

Spring-boot-route (26) Integrate webSocket

This series of articles are frequently used in the work of knowledge, after learning this series, to cope with daily development more than enough. If you want to know more, just scan the qr code below and let me know. I will further improve this series of articles!