SpringBoot整合Mail(从环境准备到编码再到调试)

标签: java  intellij idea  spring  javamail

Mail整合

sun公司提供了javaMail用来实现邮件发送,但是配置繁琐,spring中提供了javaMailSender用来简化邮件配置,springboot则提供了MailSendAutoConfiguration对邮件发送做了进一步的简化.

下面我以qq邮箱为例,进行简单的邮件发送

在这里我只将关键的代码,以及coding中的问题点进行描述,一些简单的问题就不再赘述。

pom文件

建议使用springboot的springInitializer,在io里选mail

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

application.yml

`server:
  port: 8001

spring:
  mail:
    host: smtp.qq.com
    username: 邮箱号@
    password: 密码(qq邮箱用授权码)
    properties:
      mail:
        smtp:
          auth: true
          starttls:
            enable: true
            required: true
    default-encoding: UTF-8`

授权码获取方式:网页qq邮箱>>设置>>账户>>POP3/SMTP服务 开启

1---------------------------------------------------------------------------------------------------------------------------------------
在这里插入图片描述

2---------------------------------------------------------------------------------------------------------------------------------------
在这里插入图片描述
3---------------------------------------------------------------------------------------------------------------------------------------

在这里插入图片描述
接下来会让你发短信验证之类的就不多废话了。

注意 :不要用阿里云邮箱作为服务器邮箱(会被当做垃圾邮件退回)
在这里插入图片描述

代码

MailUtil:发送邮件的类

package com.example.bootmail.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

@Component
public class MailUtil {
    @Value("${spring.mail.username}")
    private String sendForm;
    @Autowired
    private JavaMailSender javaMailSender;

    public void sendMailSimpleMail(String sendTo, String title, String content) {
        //简单邮件的发送
        SimpleMailMessage message= new SimpleMailMessage();
        message.setFrom(sendForm);
        message.setTo(sendTo);
        message.setSubject(title);
        message.setText(content);
        javaMailSender.send(message);
    }

    public void sendHtmlMail(String sendTo, String title, String content) throws MessagingException {
        MimeMessage mimeMailMessage = null;
            mimeMailMessage = javaMailSender.createMimeMessage();
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMailMessage, true);
            mimeMessageHelper.setFrom(sendForm);
            mimeMessageHelper.setTo(sendTo);
            mimeMessageHelper.setSubject(title);
            StringBuilder sb = new StringBuilder();
            sb.append("<h1>SpirngBoot测试邮件HTML</h1>")
                    .append("\"<p style='color:#F00'>"+content+"</p>")
                    .append("<p style='text-align:right'>右对齐</p>");
            mimeMessageHelper.setText(sb.toString(), true);
            javaMailSender.send(mimeMailMessage);
    }

    public void sendAttachmentMail(String sendTo, String title, String content, File file) {
        MimeMessage mimeMailMessage = null;
        try {
            mimeMailMessage = javaMailSender.createMimeMessage();
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMailMessage, true);
            mimeMessageHelper.setFrom(sendForm);
            mimeMessageHelper.setTo(sendTo);
            mimeMessageHelper.setSubject(title);
            mimeMessageHelper.setText(content);
            mimeMessageHelper.addAttachment(file.getName(), file);
            javaMailSender.send(mimeMailMessage);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}

MailController:用来接收请求的类

package com.example.bootmail.controller;

import com.example.bootmail.util.MailUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
public class MailController {
    @Autowired
    private MailUtil mailUtil;

    @PostMapping("/sendSimpleMail")
    public String sendSimpleMail(@RequestBody Map<String,String> data){

        try {
            mailUtil.sendMailSimpleMail(data.get("sendTo"), data.get("title"), data.get("content"));
            return "发送成功";
        }catch (Exception e){
            e.printStackTrace();
        }
        return "发送失败";
    }
    @PostMapping("/sendHtmlMail")
    public String sendHtmlMail(@RequestBody Map<String,String> data){
        try {
            mailUtil.sendHtmlMail(data.get("sendTo"), data.get("title"), data.get("content"));
            return "发送成功";
        }catch (Exception e){
            e.printStackTrace();
        }
        return "发送失败";
    }
}


测试方法

由于没有前端代码,所有用postman来测试
1.设置header:Content-Type application/json
模拟前端发送json数据
在这里插入图片描述
2.将要发送的son写入请求体body中
注意选择raw >> json
在这里插入图片描述

最终邮件效果

在这里插入图片描述

版权声明:本文为weixin_42539429原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_42539429/article/details/106503701