首先要在pom.xml中导入依赖,都是零几年都停更的依赖
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
然后是编写uitl类,这里为了优化用户体验,采用多线程的方式进行
package com.fan.util;
import com.fan.pojo.User;
import com.sun.mail.util.MailSSLSocketFactory;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.security.GeneralSecurityException;
import java.util.Properties;
//提高用户体验 异步处理
public class Sendmail extends Thread{
//用于给用户发送邮件的邮箱
private String from = "291418260@qq.com";
//邮箱用户名
private String username = "291418260@qq.com";
//邮箱密码
private String password = "gtbuxuuzklojbjei";
//发送邮箱的服务器地址
private String host = "smtp.qq.com";
private User user;
public Sendmail(User user){
this.user = user;
}
@Override
public void run() {
try {
Properties prop = new Properties();
prop.setProperty("mail.host","smtp");//设置邮箱服务器
prop.setProperty("mail.transport.protocol","smtp");//邮箱发送协议
prop.setProperty("mail.smtp.auth","ture");//需要验证用户名密码
//关于qq邮箱,还要设置SSL加密,加上以下代码即可
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
prop.put("mail.smtp.ssl.enable","true");
prop.put("mail.smtp.ssl.socketFactory",sf);
/*
使用JavaMail发送邮件的五个步骤
*/
//1.创建定义整个应用程序所需的环境信息的Session对象
//qq才有的一部分
Session session = Session.getDefaultInstance(prop, new Authenticator() {
@Override
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
session.setDebug(true);
//2.通过session得到transport对象
Transport ts = session.getTransport();
//3.使用邮箱的用户名和授权码连上邮件服务器
ts.connect(host,username,password);
//4.创建邮件
MimeMessage message = new MimeMessage(session);
//指明邮件发件人
message.setFrom(new InternetAddress(from));
//指明邮件的收件人
message.setRecipient(Message.RecipientType.TO, new InternetAddress(user.getEmail()));
//邮件的标题
message.setSubject("用户注册验证邮件");
String info = "欢迎注册,您的验证码为 1124 ,请输入到网页进行验证";
//邮件的文本内容
message.setContent(info,"text/html;charset=UTF-8");
//5.发送邮件
ts.sendMessage(message,message.getAllRecipients());
//关闭连接
ts.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
然后在dopost方法中调用,我在用户注册时用到了简易的邮箱验证方法,注意一定要使用start()方法,不然多线程失去了作用。
package com.fan.servlet.user;
import com.fan.pojo.User;
import com.fan.service.user.UserService;
import com.fan.service.user.UserServiceImpl;
import com.fan.util.Constant;
import com.fan.util.Sendmail;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class SecEmailServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
User user = new User();
user.setEmail(req.getParameter("email"));
UserService userService = new UserServiceImpl();
boolean flag = userService.secEmail(user.getEmail());
if (!flag){
Sendmail sendmail = new Sendmail(user);
sendmail.start();
req.getSession().setAttribute("user",user);
req.getSession().setAttribute("reg_error","邮件已发送,若为收到,请稍等片刻!");
req.getRequestDispatcher("/register.jsp").forward(req,resp);
}else {
req.getSession().setAttribute("email_error","该邮箱地址已被注册过,请直接登录!");
req.getRequestDispatcher("/sec_email.jsp").forward(req,resp);
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
同样的结尾:敬畏狂神!