How to use springboot combined with email to send verification code?

To use Spring Boot to send verification codes by email, follow these steps:

1. Add related dependencies

pom.xmlAdd Spring Boot Mail and Apache Commons Lang dependencies to the file :

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

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.9</version>
</dependency>

2. Configure SMTP mail server

Add SMTP mail server configuration in or application.propertiesfile :application.yml

# SMTP服务器地址
spring.mail.host=smtp.example.com
# SMTP服务器端口
spring.mail.port=587
# 是否启用TLS
spring.mail.properties.mail.smtp.starttls.enable=true
# SMTP服务器用户名
spring.mail.username=yourusername
# SMTP服务器密码
spring.mail.password=yourpassword
# 邮件编码
spring.mail.default-encoding=UTF-8

3. Create a mail service class

Create a mail service class to send mail, for example:

import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

@Service
public class EmailService {

    @Autowired
    private JavaMailSender mailSender;

    public void sendVerificationCode(String email) {
        String code = RandomStringUtils.randomNumeric(6);
        String subject = "Verification Code";
        String text = "Your verification code is " + code;
        sendEmail(email, subject, text);
    }

    private void sendEmail(String email, String subject, String text) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setTo(email);
        message.setSubject(subject);
        message.setText(text);
        mailSender.send(message);
    }

}

This class contains a sendVerificationCodemethod that generates a 6-digit random number verification code and sends it to the specified email address.

4. Call the mail service class

Call the method of the mail service class where the verification code needs to be sent sendVerificationCode, for example:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api")
public class MyController {

    @Autowired
    private EmailService emailService;

    @PostMapping("/verify")
    public String verifyEmail(@RequestParam String email) {
        emailService.sendVerificationCode(email);
        return "Verification code sent to " + email;
    }

}

In this example, the calling verifyEmailmethod will send a verification code to the specified email address and return a message indicating that the verification code was sent to that address.

The above are the basic steps of using Spring Boot combined with email to send the verification code, which can be modified appropriately according to the actual situation.

Guess you like

Origin blog.csdn.net/m0_72605743/article/details/129755471