验证码"/>
邮箱图片验证码
---目录
- 展示
- 依赖
- yml配置
- 邮件实现类
- 验证码工具类
展示
依赖
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-mail</artifactId></dependency>
yml配置
spring:mail:host: smtp.qq.comusername: 1034682998@qq.com(本人QQ邮箱地址)password: 开通smtp服务所生成的授权码
邮件实现类
/**
*@param mail: 接收者邮箱
*
**/@Autowiredprivate JavaMailSender mailSender; //Bean注入@Value("${spring.mail.username}")private String sender; //读取配置文件中的发送者邮箱@Overridepublic 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);//发送者邮箱,即为开通了smtp服务的邮箱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); //验证码字符加入到Session中request.getSession().setAttribute("emailCode", randomText);//输出Resource流ByteArrayOutputStream os = new ByteArrayOutputStream();ImageIO.write(verifyImg, "png", os); //指定必须为pngByteArrayResource byteArrayResource = new ByteArrayResource(os.toByteArray());//邮件图片//必须强制类型转换且contentType为image/pngQEmail_Info.addInline("emailCode", (InputStreamSource) byteArrayResource, "image/png"); //附件图片//QEmail_Info.addAttachment("attachement.png", byteArrayResource);} 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; //旋转原点的 x 坐标String ch = "";Random random = new Random();for (int i = 0; i < 4; i++) {graphics.setColor(getRandomColor());//设置字体旋转角度int degree = random.nextInt() % 30; //角度小于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;}
}
更多推荐
邮箱图片验证码
发布评论