添加依赖
<dependency><groupId>com.sun.mail</groupId><artifactId>javax.mail</artifactId><version>1.6.2</version>
</dependency>
配置邮箱
实现代码
package com.example.demo.service;
import org.springframework.stereotype.Service;import java.util.Properties;
import java.util.Random;
import javax.mail.*;
import javax.mail.internet.*;@Service
public class QQEmailSender {public void qq(String qq) {// 发件人邮箱地址和授权码String from ="your_email@qq.com";String password = "your_authorization_code";// 收件人电子邮箱String to = qq;// 指定发送邮件的主机为 smtp.qq.comString host = "smtp.qq.com"; //QQ 邮件服务器// 获取系统属性Properties properties = System.getProperties();// 设置邮件服务器properties.setProperty("mail.smtp.host", host);properties.put("mail.smtp.auth", "true");// 获取默认的 Session 对象。// 获取默认session对象Session session = Session.getDefaultInstance(properties,new Authenticator(){public PasswordAuthentication getPasswordAuthentication(){return new PasswordAuthentication(from, password); //发件人邮件用户名、授权码}});// 设置debug模式便于调试:session.setDebug(true);try{// 创建默认的 MimeMessage 对象。MimeMessage message = new MimeMessage(session);// Set From: 头部头字段message.setFrom(new InternetAddress(from));// Set To: 头部头字段message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));// Set Subject: 头字段message.setSubject("大可","UTF-8");// 发送 HTML 消息, 可以插入html标签String generatedCode = random1(); // 假设后台生成的验证码String emailBody = vericodeHtml.replace(":data=\"123456\"", ":data=\"" + generatedCode + "\"").replace("1EM456", generatedCode); //将发送页面的验证码改为后台生成的验证码message.setText(emailBody, "UTF-8", "html");// 发送消息Transport.send(message);System.out.println("Sent message successfully....");}catch (MessagingException mex) {mex.printStackTrace();}}//生成6位数 验证码public static String random1() {String code = "";Random rd = new Random();for (int i = 0; i < 6; i++) {int r = rd.nextInt(10); //每次随机出一个数字(0-9)code = code + r; //把每次随机出的数字拼在一起}System.out.println(code);return code;}public static String vericodeHtml = "<!DOCTYPE html>\n" +"<html lang=\"en\">\n" +"<head>\n" +" <meta charset=\"UTF-8\">\n" +" <title>邮箱验证码</title>\n" +" <style>\n" +"\n" +" .main {\n" +" margin: 10px auto;\n" +" width: 520px;\n" +"\n" +" border-top: 4px solid #9373EE;\n" +" padding: 24px 24px 40px;\n" +" border-radius:0 0 8px 8px;\n" +" box-shadow: 0px 0px 1px;\n" +" }\n" +"\n" +" .title {\n" +" margin: 80px auto 32px;\n" +" font-size: 32px;\n" +" font-weight: 600;\n" +" line-height: 45px;\n" +" letter-spacing: 0px;\n" +"\n" +" }\n" +"\n" +" .note {\n" +" margin: 0 auto;\n" +" font-size: 18px;\n" +" line-height: 1.4;\n" +" left: 0px;\n" +" top: 77px;\n" +" font-weight: 400;\n" +" }\n" +"\n" +" .code {\n" +" padding: 16px;\n" +" text-align: center;\n" +" background: rgba(147, 115, 238, 0.04);\n" +" border-radius: 4px;\n" +" font-weight: 600;\n" +" font-size: 24px;\n" +" line-height: 140%;\n" +" color: #9373EE;\n" +" margin: 24px 0;\n" +" letter-spacing: 1px;\n" +" }\n" +"\n" +" .claim ul {\n" +" margin-top: 34px;\n" +" margin-bottom: 40px;\n" +" font-size: 13px;\n" +" line-height: 1.6;\n" +" color: #5c5c5c;\n" +" padding: 25px 0;\n" +"\n" +" }\n" +"\n" +" .claim ul li {\n" +" color: rgba(24, 24, 25, 0.42);\n" +" line-height: 30px;\n" +" }\n" +"\n" +" .footer {\n" +" font-size: 13px;\n" +" line-height: 1.6;\n" +" color: #5c5c5c;\n" +" padding: 25px 0\n" +" }\n" +" .title,.note,.claim,.footer {\n" +" text-align: center;\n" +" }\n" +" </style>\n" +"</head>\n" +"<body>\n" +"<div class=\"main\">\n" +" <div class=\"title\">邮箱账号验证码</div>\n" +" <div class=\"note\">你正在进行邮箱验证操作,验证码为:</div>\n" +" <div class=\"code\" :data=\"123456\">1EM456</div>\n" +"\n" +" <div class=\"claim\">\n" +" <ul style=\"list-style: none;\">\n" +" <li style=\"list-style: none;\">此验证码 15 分钟内有效</li>\n" +" <li style=\"list-style: none;\">如非本人操作</li>\n" +" <li style=\"list-style: none;\">转给他人将导致账号被盗和个人信息泄漏,谨防诈骗</li>\n" +" </ul>\n" +" </div>\n" +"\n" +" <div class=\"footer\">\n" +" <a href=\"\" target=\"_blank\" style=\"color: #9373EE; text-decoration: none;\"></a>\n" +" </div>\n" +"</div>\n" +"</body>\n" +"</html>";
}