引言
发送邮件依赖于JavaMailSender类的send()方法来实现。
SimpleMailMessage:封装简单邮件的Bean。
MimeMessage:封装复杂邮件的Bean,但其设置邮件的属性则需要依赖于MimeMessageHelper类的辅助。
1、发送简单邮箱(不含图片、附件等,就纯文本)
导入依赖项:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
配置application.properties文件
spring.mail.username=test@qq.com //你的邮箱
spring.mail.password=xxx //授权码
spring.mail.host=smtp.qq.com //邮箱的服务器
实现代码
package com.example.practice;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
@SpringBootTest
public class test {
@Autowired
private JavaMailSender sender;
private String subject = "这是标题";
private String text = "这是内容";
private String to = "test@qq.com"; //发给谁
private String from = "test@qq.com"; //谁发的
@Test
public void test_demo(){
SimpleMailMessage mail = new SimpleMailMessage();
mail.setSubject(subject);
mail.setText(text);
mail.setTo(to);
mail.setFrom(from);
sender.send(mail);
System.out.println("发送成功");
}
}
2、发送复杂邮件(含图片、附件)
package com.example.practice;
import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import java.io.File;
@SpringBootTest
public class test {
@Autowired
private JavaMailSender sender;
private String subject = "这是标题";
private String text = "这是内容";
private String to = "test@qq.com"; //发给谁
private String from = "test@qq.com"; //谁发的
@Test
public void test_demo_2() throws MessagingException {
MimeMessage mail = sender.createMimeMessage();
//第二个true代表开启multiplat
MimeMessageHelper helper = new MimeMessageHelper(mail, true);
//使用io流引入文件
File file = new File("D://1.txt");
helper.setSubject(subject);
//开启html解析标签,让<img />html标签可以得到解析
helper.setText(text + "<img src='https://img-s-msn-com.akamaized.net/tenant/amp/entityid/BB1jCBFx.img?w=768&h=486&m=6&x=667&y=132&s=230&d=230' />",true);
helper.setTo(to);
helper.setFrom(from);
//addAttachment用于发送附件
helper.addAttachment(file.getName(),file);
//发送邮件
sender.send(mail);
System.out.println("发送成功");
}
}
3、图片验证的实现
引入hutool工具包和redis(用于暂时存储图片验证码)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.26</version>
</dependency>
编写代码
@RestController
@RequestMapping("captcha")
@CrossOrigin
public class CaptchaController {
@Autowired
private RedisTemplate redisTemplate;
@GetMapping
public Result makeCaptcha() {
//创建一个图形验证码图片
LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(130, 60, 4, 4);
//获取验证码
String code = lineCaptcha.getCode();
//使用redisTemplate把code的值存入redis数据库
redisTemplate.opsForValue().set("code", code);
//设置元素的存活时间,为300秒
redisTemplate.expire("code",300, TimeUnit.SECONDS);
//打印一下code
System.out.println("code = " + code);
//转Base64编码用于网络传输
String imageBase64Data = lineCaptcha.getImageBase64Data();
//返回给前端
return new Result("200","成功" , imageBase64Data);
}
}
前端使用axios请求该图片
<template>
<div>
<div><el-input v-model="captcha" style="width: 240px" placeholder="验证码" /></div>
<div><img :src="captchaSRC" @click="load"></div>
</div>
</template>
<script setup>
import { ref } from "vue";
import axios from "axios"
const captcha = ref('');
let captchaSRC = ref('');
function getCaptchaSRC(){
axios.get('http://localhost:7777/captcha').then(res => {
if(res.data.code === '200'){
//后端传过来的一个Base64的编码,浏览器可以解析出来还原成图片形式
captchaSRC.value = res.data.data;
}
})
}
//调用方法,加载图片
getCaptchaSRC();
//定义load函数,点击图片即更新图片
const load = () => {
axios.get('http://localhost:7777/captcha').then(res => {
if(res.data.code === '200'){
captchaSRC.value = res.data.data;
}
})
}
</script>