第一种:邮箱验证方法:
导入依赖:
<!-- mail依赖(发送短信的依赖) -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
<!-- <version>3.1.5</version>-->
</dependency>
<!-- thymeleaf 依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<!-- <version>3.1.5</version>-->
</dependency>
代码的实现
创建一个application.yml 文件
小提示:
一般用邮箱发送短信用 QQ 邮箱;(163 邮箱会过滤掉垃圾文件),所以 QQ 文件比较稳定
spring:
mail:
#邮件服务器地址
host: smtp.qq.com
#协议
protocol: smtp
#编码格式
default-encoding: utf-8
#授权码
password: jhgqhedbdyvcdhdj
#邮箱名称
username: 2929119150@qq.com
#端口号
port: 587
实现的代码
//邮箱相关的配置(内容,收件人,等附件信息)
@Resource
private JavaMailSender javaMailSender;
//邮箱相关的配置(邮箱服务主机名称,端口等)
@Resource
private MailProperties mailProperties;
//模版
@Resource
private TemplateEngine templateEngine;
public void EmailTopicListener(TEmployee tEmployee){
System.out.println("获取的结果:"+tEmployee);
/**
* 业务内容
*/
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage);
//添加内容
try {
//发件人
mimeMessageHelper.setFrom(mailProperties.getUsername());
System.out.println("发件人:"+mailProperties.getUsername());
//收件人
mimeMessageHelper.setTo(tEmployee.getEmail());
//主题
mimeMessageHelper.setSubject("入职欢迎邮件");
//发送日期
mimeMessageHelper.setSentDate(new Date());
//邮箱页面模板(thymeleaf模版)
Context context =new Context();
context.setVariable("name",tEmployee.getName());
context.setVariable("posName",tEmployee.gettPosition().getName());
context.setVariable("joblevelName",tEmployee.gettJoblevel().getName());
context.setVariable("departmentName",tEmployee.gettDepartment().getName());
String email = templateEngine.process("mail", context);
//邮件内容
mimeMessageHelper.setText(email,true);
//发送邮件
javaMailSender.send(mimeMessage);
} catch (MessagingException e) {
throw new RuntimeException(e);
}
第二种:邮箱验证方法
导入依赖:
<!--邮箱-->
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.2</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
代码的实现:
// 收件人电子邮箱
String to = toEail;
// 发件人电子邮箱
String from = "2929119150@qq.com";
// 生成的授权码
String password = "jhgqhedbdyvcdhdj";
// 指定发送邮件的主机为 smtp.qq.com
String 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 = code; // 假设后台生成的验证码
String emailBody = vericodeHtml.replace(":data=\"123456\"", ":data=\"" + generatedCode + "\"").replace("1EM456", generatedCode); //将发送页面的验证码改为后台生成的验证码
message.setText(emailBody, "UTF-8", "html");
try {
// 发送消息
Transport.send(message);
//休眠
TimeUnit.MILLISECONDS.sleep(100);
//todo 如果发送成功将code保存到redis,防止恶意请求
log.info("---red保存成功----");
} catch (InterruptedException e) {
return false;
}
return true;
}catch (MessagingException mex) {
return false;
}
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=\"https://blog.csdn.net/qq_62254095?spm=1018.2226.3001.5343\" target=\"_blank\" style=\"color: #9373EE; text-decoration: none;\">空门集团</a> - 昨夜西风凋碧树,独上高楼,望尽天涯路。 --蝶恋花·槛菊愁烟兰泣露\n" +
" </div>\n" +
"</div>\n" +
"</body>\n" +
"</html>";