Springboot整合发送邮箱功能
1.引入的依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
2.创建一个组件用来产生邮箱验证码等特殊内容
package com.example.demo.utils;
import com.baomidou.mybatisplus.extension.api.R;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;
public class CreateVerifyCodeImage {
private static int WIDTH=90;
private static int HEIGHT=35;
private static int FONT_SIZE=20;
private static char[] verifyCode;
private static BufferedImage verifyCodeImage;
public static BufferedImage getVerifyCodeImage(){
verifyCodeImage = new BufferedImage(WIDTH,HEIGHT,BufferedImage.TYPE_INT_BGR);
Graphics graphics = verifyCodeImage.getGraphics();
verifyCode = generateCheckCode();
drawBackground(graphics);
drawRands(graphics,verifyCode);
graphics.dispose();
return verifyCodeImage;
}
private static void drawBackground(Graphics graphics) {
graphics.setColor(Color.white);
graphics.fillRect(0,0,WIDTH,HEIGHT);
for(int i=0 ; i<200; i++){
int x = (int)(Math.random()*WIDTH);
int y = (int)(Math.random()+HEIGHT);
graphics.setColor(getRamdomColor());
graphics.drawOval(x,y,1,1);
}
}
private static void drawRands(Graphics graphics, char[] verifyCode) {
graphics.setFont(new Font("Console",Font.BOLD , FONT_SIZE));
for(int i=0; i<verifyCode.length ;i++){
graphics.setColor(getRamdomColor());
graphics.drawString(""+verifyCode[i],i*FONT_SIZE+10,25);
}
}
private static Color getRamdomColor() {
Random random = new Random();
return
new Color(random.nextInt(220),random.nextInt(220),random.nextInt(220));
}
private static char[] generateCheckCode() {
String chars= "0123456789"+"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char[]rands = new char[4];
for(int i= 0 ;i<4;i++){
int rand = (int)(Math.random()*(10+26));
rands[i] = chars.charAt(rand);
}
return rands;
}
public static char[] getVerifyCode(){
return verifyCode;
}
}
3.实现发送邮件功能的Controller
package com.example.demo.controller;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.service.impl.MyUserDetailsService;
import com.example.demo.utils.CreateVerifyCodeImage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.awt.image.BufferedImage;
@RestController
public class EmailController {
@Autowired
JavaMailSenderImpl javaMailSender;
@Autowired
private MyUserDetailsService myUserDetailsService;
@ResponseBody
@RequestMapping("/email/register")
public Object sendEmailForRegisterUser(HttpServletRequest request){
JSONObject jsonObject = new JSONObject();
String email = request.getParameter("email");
boolean exsitEmail = myUserDetailsService.existEmail(email);
if(exsitEmail){
jsonObject.put("code",0);
jsonObject.put("message","该邮箱已经被注册");
return jsonObject;
}
BufferedImage verifyCodeImage = CreateVerifyCodeImage.getVerifyCodeImage();
String verifyCode = String.valueOf(CreateVerifyCodeImage.getVerifyCode());
request.getSession().setAttribute("verifyCode",verifyCode);
SimpleMailMessage message = new SimpleMailMessage();
message.setSubject("账号注册验证");
message.setText("您好,您的验证码是:"+verifyCode);
message.setTo(email);
message.setFrom("qin7899@qq.com");
javaMailSender.send(message);
jsonObject.put("code",1);
jsonObject.put("message","已经发送邮件");
return jsonObject;
}
}
4.配置发送邮件的邮箱
QQ方面
spring.mail.username=邮箱账号
spring.mail.password=个人邮箱授权码
spring.mail.host=smtp.qq.com
其他邮箱也可以用于发送邮件
版权声明:本文为weixin_43948626原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。