注册发送邮件验证

package com.changbei.jeesite.modules.school.utils;

import com.sun.mail.util.MailSSLSocketFactory;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

public class MailUtil implements Runnable{
private String email;// 收件人邮箱
private String code;// 激活码

public MailUtil(String email, String code) {
this.email = email;
this.code = code;
}

@Override
public void run() {
// 1.创建连接对象javax.mail.Session
// 2.创建邮件对象 javax.mail.Message
// 3.发送一封激活邮件
String from = "[email protected]";// 发件人电子邮箱
String host = "smtp.qq.com"; // 指定发送邮件的主机smtp.qq.com(QQ)|smtp.163.com(网易)

Properties properties = System.getProperties();// 获取系统属性

properties.setProperty("mail.smtp.host", host);// 设置邮件服务器
properties.setProperty("mail.smtp.auth", "true");// 打开认证

try {
//QQ邮箱需要下面这段代码,163邮箱不需要
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
properties.put("mail.smtp.ssl.enable", "true");
properties.put("mail.smtp.ssl.socketFactory", sf);


// 1.获取默认session对象
Session session = Session.getDefaultInstance(properties, new Authenticator() {
@Override
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("43059****@qq.com", "****"); // 发件人邮箱账号、授权码
}
});

// 2.创建邮件对象
Message message = new MimeMessage(session);
// 2.1设置发件人
message.setFrom(new InternetAddress(from));
// 2.2设置接收人
message.addRecipient(Message.RecipientType.TO, new InternetAddress(email));
// 2.3设置邮件主题
message.setSubject("账号激活");
// 2.4设置邮件内容
String content = "<html><head></head><body><h1>这是一封激活邮件,激活请点击以下链接</h1><h3><a href='http://localhost:8088/sc/xx?code="
+ code + "'>http://localhost:8080/sc/xx?code=" + code
+ "</href></h3></body></html>";
message.setContent(content, "text/html;charset=UTF-8");
// 3.发送邮件
Transport.send(message);
System.out.println("邮件成功发送!");
} catch (Exception e) {
e.printStackTrace();
}
}
}


---------------------------

@ApiOperation(value="注册用户", notes="用户注册111")
@ResponseBody
@RequestMapping(value = "register",method = RequestMethod.POST)
public String register (ScUserDTO dto, HttpServletRequest request, HttpServletResponse response, Model model){
//查询用户名是否已存在
List<ScUser> list = scService.findList(new ScUser());
for (ScUser u:list) {
if(u.getLoginName().equals(dto.getLoginName())){
model.addAttribute("msg","用户名已存在");
return "jsp/school/login/login";
}
}
ScUser scUser = scService.create(dto);
if(null != scUser){
//保存成功则通过线程的方式给用户发送一封邮件
// new Thread(new MailUtil(scUser.getScEmail(), scUser.getRemarks())).start();

//使用线程池(上面的单线程不安全,量大容易出问题)
ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("demo-pool-%d").build();
ExecutorService singleThreadPool = new ThreadPoolExecutor(1, 1,0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>(1024), namedThreadFactory, new ThreadPoolExecutor.AbortPolicy());

singleThreadPool.execute(new MailUtil(scUser.getScEmail(), scUser.getRemarks()));
singleThreadPool.shutdown();
}
model.addAttribute("msg","注册成功,请进入邮箱对账号进行激活");
model.addAttribute("user",dto);
return "jsp/school/login/tiao";
}

猜你喜欢

转载自www.cnblogs.com/aqihao/p/9686850.html