网站注册发送邮件
由于邮件的发送需要一定的时间,页面难以及时的跳转,为了提高用户的体验,故采取多线程的形式
所需jar包
servlet-api;mail;activation
准备注册页面
<%@ page contentType="text/html; charset = utf-8" pageEncoding="UTF-8"
language="java" %>
<html>
<head>
<title>注册</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/send.do" method="post">
<p>用户名:<input type="text" name="username"></p>
<p>密码:<input type="password" name="pwd"></p>
<p>邮箱:<input type="email" name="email"></p>
<p><input type="submit"> | <input type="reset"></p>
</form>
</body>
</html>
编写User实体类
package com.feng.pojo;
import java.io.Serializable;
public class User implements Serializable {
private String username;
private String pwd;
private String email;
public User() {
}
public User(String username, String pwd, String email) {
this.username = username;
this.pwd = pwd;
this.email = email;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "pojo{" +
"username='" + username + '\'' +
", pwd='" + pwd + '\'' +
", email='" + email + '\'' +
'}';
}
}
编写多线程邮件发送
邮件发送的具体步骤可参照:https://blog.csdn.net/shuati2000/article/details/120757667
package com.feng.util;
import com.feng.pojo.User;
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 Sendmail extends Thread{
private User user;
public Sendmail(User user){
this.user = user;
}
@Override
public void run() {
try {
Properties prop = new Properties();
prop.setProperty("mail.host", "smtp.qq.com"); //这只QQ邮箱服务器
prop.setProperty("mail.transport.protocol", "smtp"); //邮件发送协议
prop.setProperty("mail.smtp.auth", "true"); //验证用户名密码
//关于QQ邮箱需要设置SSL加密
MailSSLSocketFactory sf = null;
sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
prop.put("mail.smtp.ssl.enable", "true");
prop.put("mail.smtp.socketFactory", sf);
//1.创建定义整个应用程序所需的环境信息的Session对象
Session session = Session.getDefaultInstance(prop, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("1002960106@qq.com", "授权码");
}
});
//开启Session的Dubug模式,这样就可以查看程序发送Email的状态
session.setDebug(true);
//2.通过Session得到transport
Transport ts = session.getTransport();
//3.使用邮件的用户名和授权码连上邮件的服务器
ts.connect("smtp.qq.com","1002960106@qq.com","授权码");
//4.创建邮件
MimeMessage message = new MimeMessage(session);
//指明发件人
message.setFrom(new InternetAddress("1002960106@qq.com"));
//指明邮件的收件人
message.setRecipient(Message.RecipientType.TO,new InternetAddress(user.getEmail());
//邮件的标题
message.setSubject("Good Study Day Day Up");
//邮件的文本内容
message.setContent("欢迎您的注册:\n"+"您的用户名为:"+user.getUsername()+"密码为:"+user.getPwd(),"text/html;charset=UTF-8");
//5.发送邮件
ts.sendMessage(message,message.getAllRecipients());
//6.关闭连接
ts.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
调用多线程进行邮件发送
package com.feng.servlet;
import com.feng.pojo.User;
import com.feng.util.Sendmail;
import com.sun.mail.util.MailSSLSocketFactory;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.Properties;
public class loginSend extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username = req.getParameter("username");
String pwd = req.getParameter("pwd");
String email = req.getParameter("email");
User user = new User(username, pwd, email);
Sendmail sendmail = new Sendmail(user);
sendmail.start();
req.setAttribute("msg","欢迎您的加入,注册成功,我们已经发送了一份带有注册信息的邮件,请注意查收!");
req.getRequestDispatcher("sucess.jsp").forward(req,resp);
}
}
成功跳转页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>注册成功</title>
</head>
<body>
${msg}
</body>
</html>
在web.xml对servlet进行组测
大功告成!!!赶紧来进行邮件发送吧