How to quickly create an email task in SpringBoot project

How to quickly create an email task in SpringBoot project

1. First create a springboot project

提示:选择的java版本不要太高

Insert image description here

I chose Spring Web here

Insert image description here


2.Introduce related dependencies

只需要引入这一个依赖即可

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

3. Analyze source code

1. First we introduce the dependency and find the mail automatic configuration name according to the name - MailSenderAutoConfiguration

Please refer to SpringBoot automatic configuration principle

Insert image description here

2. After coming in, we see MailProperties.class, drill in, and find the content required to be configured.

Insert image description here

3. See that you need to configure the host name, user name, password, etc.

Insert image description here

4. Return to the previous level and drill into MailSenderJndiConfiguration

Insert image description here

You will find that there is an "email sending implementation class", we can call it

Insert image description here


4. Get email code

这里使用的qq邮箱

1. Open QQ mailbox, click Settings - Select Account - Click the first service to open

Insert image description here

2. Click to open and send a text message, and then get the authorization code

Insert image description here

3. Copy and paste the authorization code into spring.mail.password= in the configuration file, as shown in point 5


5.Configuration content

这里使用的是application.properties

#邮箱地址
spring.mail.username=2787567683@qq.com
#邮箱密码(授权码自行获取)
spring.mail.password=gsfqpsmmuwnfdhce
#主机名
spring.mail.host=smtp.qq.com

#开启加密验证
spring.mail.properties.mail.smtp.ssl.enable=true

6. Write code

这里我将发送邮件的功能封装为一个工具类

1.Write ToolUtil

/**
 * @Author liuyun
 * @Date 2023/3/6 11:16
 * @Version 1.0
 * 发送邮件工具类
 */

@Configuration//申明该配置
public class ToolUtil {
    
    
    
    //引入邮件发送实现类
    @Autowired
    JavaMailSenderImpl javaMailSender;

    /**
     *
     * @param html:是否支持多文本上传
     * @param subject:邮件标题
     * @param text:邮件主要内容
     * @param jpg:附件
     * @param jpgWay:附件路径
     * @param from:发件人邮箱地址
     * @param to:收件人邮箱地址
     * @throws MessagingException
     * 发送邮件
     */
    public void sendMail(Boolean html, String subject, String text, List<String> jpg, List<String> jpgWay, String from, String to) throws MessagingException {
    
    
        //一个复杂的邮件
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //组装
        MimeMessageHelper mailMessage = new MimeMessageHelper(mimeMessage,html);

        //正文
        mailMessage.setSubject(subject);
        mailMessage.setText(text);

        //附件,遍历得到的附件内容
        for (int i = 0; i < jpg.size(); i++) {
    
    
            for (int j = 0; j < jpgWay.size(); j++) {
    
    
                if (i==j){
    
    
                    mailMessage.addAttachment(jpg.get(i),new File(jpgWay.get(i)));
                }
            }
        }

        mailMessage.setFrom(from);
        mailMessage.setTo(to);


        javaMailSender.send(mimeMessage);
    }
}

2. Retrieval method

 @RequestMapping("/mailSend")
    public String mailSend() throws MessagingException {
    
    
		
		//邮件标题
        String subject = "入职申请";
        //邮件内容
        String text = "我申请入职腾讯高管";
        //发件人邮箱地址
        String from = "[email protected]";
        //收件人邮箱地址
        String to = "[email protected]";
        //附件内容
        List<String> jpgs = new ArrayList<>();
        jpgs.add("7.png");
        jpgs.add("q.jpeg");
        //附件路径
        List<String> jpgWays = new ArrayList<>();
        jpgWays.add("C:\\Users\\internet\\Desktop\\folder\\7.png");
        jpgWays.add("C:\\Users\\internet\\Desktop\\folder\\q.jpeg");

		//调取方法,将内容传进去
        toolUtil.sendMail(true,subject,text,jpgs,jpgWays,from,to);

        return "Success";
    }

7. Start, access, and successfully receive the email

Insert image description here
received e-mail

Insert image description here

This is the email task function; reference


Hope it helps you

~感谢您的光临~

Insert image description here

Guess you like

Origin blog.csdn.net/m0_50762431/article/details/129359120