展示
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/b1320d732356faa8f51bb327d24a4415.jpeg#pic_center)
依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
yml配置
spring:
mail:
host: smtp.qq.com
username: 1034682998@qq.com(本人QQ邮箱地址)
password: 开通smtp服务所生成的授权码
邮件实现类
@Autowired
private JavaMailSender mailSender;
@Value("${spring.mail.username}")
private String sender;
@Override
public Result mail(String mail, HttpServletRequest request) {
MimeMessage QEmail = null;
try {
int width = 200;
int height = 69;
QEmail = mailSender.createMimeMessage();
MimeMessageHelper QEmail_Info = new MimeMessageHelper(QEmail, true);
QEmail_Info.setFrom(sender);
QEmail_Info.setTo(mail);
QEmail_Info.setSubject("用户注册");
QEmail_Info.setText("注册验证码为");
QEmail_Info.setText("<html><body>请输入图片中的验证码<hr>图片:<img src='cid:emailCode' style =
'width:600px;height:300px;border:1px solid #000;'/></body></html>", true);
BufferedImage verifyImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
String randomText = VerifyCode.drawRandomText(width, height, verifyImg);
request.getSession().setAttribute("emailCode", randomText);
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(verifyImg, "png", os);
ByteArrayResource byteArrayResource = new ByteArrayResource(os.toByteArray());
QEmail_Info.addInline("emailCode", (InputStreamSource) byteArrayResource, "image/png");
} catch (Exception e) {
return "失败";
}
mailSender.send(QEmail);
return "成功");
}
验证码工具类
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;
public class VerifyCode {
public static String drawRandomText(int width, int height, BufferedImage verifyImg) {
Graphics2D graphics = (Graphics2D) verifyImg.getGraphics();
graphics.setColor(Color.WHITE);
graphics.fillRect(0, 0, width, height);
graphics.setFont(new Font("微软雅黑", Font.BOLD, 40));
StringBuffer sBuffer = new StringBuffer();
int x = 10;
String ch = "";
Random random = new Random();
for (int i = 0; i < 4; i++) {
graphics.setColor(getRandomColor());
int degree = random.nextInt() % 30;
ch = getText();
sBuffer.append(ch);
graphics.rotate(degree * Math.PI / 180, x, 45);
graphics.drawString(ch, x, 45);
graphics.rotate(-degree * Math.PI / 180, x, 45);
x += 48;
}
for (int i = 0; i < 6; i++) {
graphics.setColor(getRandomColor());
graphics.drawLine(random.nextInt(width), random.nextInt(height),
random.nextInt(width), random.nextInt(height));
}
for (int i = 0; i < 30; i++) {
int x1 = random.nextInt(width);
int y1 = random.nextInt(height);
graphics.setColor(getRandomColor());
graphics.fillRect(x1, y1, 2, 2);
}
return sBuffer.toString();
}
private static Color getRandomColor() {
Random ran = new Random();
Color color = new Color(ran.nextInt(256),
ran.nextInt(256), ran.nextInt(256));
return color;
}
private static String getText() {
Random random = new Random();
String baseNumLetter = "123456789abcdefghijklmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
int dot = random.nextInt(baseNumLetter.length());
String ch = baseNumLetter.charAt(dot) + "";
return ch;
}
}