1.特别注意
isELIgnored="false"
如果没有这个El表达式无法识别
2.pre work
pox.xml
junit junit 3.8.1 test javax.servlet javax.servlet-api 4.0.1 javax.mail 1.4.7 javax.activation activation 1.1.1 org.projectlombok lombok 1.18.24 provided
3.jsp
- <%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
-
- <html>
- <body>
- <h2>注册</h2>
- <form action="${pageContext.request.contextPath}/RegisterServlet.do" method="post">
- username:<input type="text" name="username"><br>
- password:<input type="password" name="psw"><br>
- email:<input type="email" name="email"><br>
- <input type="submit" value="register">
- </form>
- </body>
- </html>
info.jsp
${msg}
4.servlet
- package com.qing.servlet;
-
- import com.qing.pojo.user;
- import com.qing.utils.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 RegisterServlet extends HttpServlet {
- @Override
- protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- //接收用户,封装对象
- String name=req.getParameter("username");
- String psw=req.getParameter("psw");
- String email=req.getParameter("email");
- System.out.println(name+" "+psw+" "+email);
- //用线程发送邮件,避免耗时过多,提高体验
- user user=new user(name,psw,email);
- SendMail send=new SendMail(user);
- send.start();
-
- req.setAttribute("msg","注册成功,邮件已发送,请注意查收!");
- req.getRequestDispatcher("info.jsp").forward(req,resp);
- }
-
- @Override
- protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- }
- }
5.utils
- package com.qing.utils;
- import com.qing.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 String from="@qq.com";
- //邮箱用户名
- private String name="@qq.com";
- //授权码
- private String password="授权码";
- //发送邮件的服务器地址
- private String host="smtp.qq.com";
-
- private user user;
- public SendMail(user user){
- this.user=user;
- }
- //重写run
- @Override
- public void run(){
- try{
- Properties prop=new Properties();
- prop.setProperty("mail.host",host);//设置qq邮件服务器
- prop.setProperty("mail.transport.protocol","smtp");//邮件发送协议
- prop.setProperty("mail.smtp.auth","true");//验证用户账号密码
-
- //qq 加密ssl
- MailSSLSocketFactory sf=new MailSSLSocketFactory();
- sf.setTrustAllHosts(true);
- prop.put("mail.smtp.ssl.enable","true");
- prop.put("mail.smtp.ssl.socketFactory",sf);
-
- Session session=Session.getDefaultInstance(prop, new Authenticator() {
-
- protected PasswordAuthentication getPasswordAuthentication() {
- return new PasswordAuthentication("@qq.com","授权码");
- }
- });
- session.setDebug(true);
- Transport ts=session.getTransport();
- ts.connect(host,name,password);
-
- MimeMessage message=new MimeMessage(session);
- //指明发件人
- message.setFrom(new InternetAddress(from));
- //收件人
- message.setRecipient(Message.RecipientType.TO,new InternetAddress(user.getEmail()));
- //邮件标题
- message.setSubject("用户注册邮件");
-
- String info="注册成功!用户名:"+user+" 密码:"+user.getPsw();
-
- message.setContent(info,"text/html;charset=UTF-8");
- message.saveChanges();
- //5.发送邮件
- ts.sendMessage(message,message.getAllRecipients());
- //6.关闭连接
- ts.close();
-
-
- } catch (Exception e) {
- throw new RuntimeException(e);
- } }}