邮箱发送验证码
2023-06-06 本文已影响0人
ml66
pom.xml文件中引入依赖jar包
<!-- Javamail -->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.4</version>
</dependency>
<!--引入redis依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
package com.creatunion.portal.common.utils;
import javax.mail.Address;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
public class MailUtil {
/**
* 实现邮件发送的方法
*
* @param to 邮件的接受者
* @param subject 主体
* @param content 正文
* @throws Exception
*/
public static void sendMail(String to, String subject, String content) throws Exception {
Properties props = new Properties();
props.setProperty("mail.smtp.host", "smtp.163.com"); //设置主机地址 smtp.qq.com smtp.sina.com
props.setProperty("mail.smtp.auth", "true");//认证
//阿里云服务器禁用25端口,所以服务器上改为465端口
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.setProperty("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.socketFactory.port", "465");
//2.产生一个用于邮件发送的Session对象
Session session = Session.getInstance(props);
//3.产生一个邮件的消息对象
MimeMessage message = new MimeMessage(session);
//4.设置消息的发送者
Address fromAddr = new InternetAddress("15711200144@163.com");
message.setFrom(fromAddr);
//5.设置消息的接收者
Address toAddr = new InternetAddress(to);
//TO 直接发送 CC抄送 BCC密送
message.setRecipient(MimeMessage.RecipientType.TO, toAddr);
//6.设置主题
message.setSubject(subject);
//7.设置正文
message.setText(content);
//8.准备发送,得到火箭
Transport transport = session.getTransport("smtp");
//9.设置火箭的发射目标
transport.connect("smtp.163.com", "15711200144@163.com", "CTNOCVIAYAYTXRWD");
//10.发送
transport.sendMessage(message, message.getAllRecipients());
//11.关闭
transport.close();
}
public static void main(String[] args) throws Exception {
String code = Utils.getRandom(6);
MailUtil.sendMail("499722834@qq.com", "测试发送邮件",
"欢迎使用石家庄市正定新区社保中心网上服务平台,您的验证码是:" + code +
",该验证码15" +
"分钟有效!");
System.out.println("发送成功!!!");
}
}
public class Utils {
public static String getRandom(Integer length) {
String num = "";
for (int i = 0; i < length; i++) {
num = num + String.valueOf((int) Math.floor(Math.random() * 9 + 1));
}
return num;
}
}
public Result sendCodeToEmail(String email) {
try {
if (org.springframework.util.StringUtils.isEmpty(email)) {
return new Result(false, ResultCode.ERROR_CODE.getCode(),
ResultCode.ERROR_CODE.getMessage());
}
if (!email.matches(REGEX_EMAIL)) {
return new Result(false, ResultCode.ERROR_CODE.getCode(),
ResultCode.ERROR_CODE.getMessage());
}
//邮件主题
String subject = "欢迎使用石家庄市正定新区社保中心网上服务平台";
//验证码
String code = Utils.getRandom(6);
//邮件内容
String text = "欢迎使用石家庄市正定新区社保中心网上服务平台,您的验证码为:" + code + ",该验证码15分钟内有效,请勿泄露给其他人!!";
MailUtil.sendMail(email, subject, text);
//将验证码以邮箱为key,验证码为value存入redis,15分钟有效
stringRedisTemplate.opsForValue().set(email, code, 60 * 15, TimeUnit.SECONDS);
return new Result(true, ResultCode.SUCCESS_CODE.getCode(), ResultCode.SUCCESS_CODE.getMessage());
} catch (Exception e) {
e.printStackTrace();
return new Result(false, ResultCode.ERROR_CODE.getCode(),
ResultCode.ERROR_CODE.getMessage());
}
}