注册页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/doregister" method="post">
用户名: <input type="text" name="name"><br>
年龄:<input type="text" name="age"><br>
email:<input type="text" name="email"><br>
<input type="submit" value="注册">
</form>
</body>
</html>
用户pojo
package pojo;
import jdk.nashorn.internal.objects.annotations.Constructor;
public class User {
private String name;
private int age;
private String email;
public User() {
}
public User(String name, int age, String email) {
this.name = name;
this.age = age;
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "user{" +
"name='" + name + '\'' +
", age=" + age +
", email='" + email + '\'' +
'}';
}
}
发送邮件utill
package mailUtill;
import com.sun.mail.util.MailSSLSocketFactory;
import pojo.User;
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;
}
public void run() {
Properties prop =null;
MailSSLSocketFactory sf = null;
try {
prop = new Properties();
prop.setProperty("mail.host","smtp.qq.com");
prop.setProperty("mail.transport.protocol","smtp");
prop.setProperty("mail.stmp.auth","true");
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() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("117513@qq.com", "邮箱序列号");
}
});
session.setDebug(true);
Transport ts = session.getTransport();
ts.connect("smtp.qq.com","117513@qq.com","邮箱序列号");
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("1175135@qq.com"));
message.setRecipient(Message.RecipientType.TO,new InternetAddress(user.getEmail()));
message.setSubject("注册成功");
message.setContent("用户名为"+user.getName()+"您已成功注册了我们的网站","text/html;charset=UTF-8");
ts.sendMessage(message,message.getAllRecipients());
ts.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
注册表单处理
package com.nn.servlet;
import mailUtill.sendMail;
import pojo.User;
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 doRegister extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String name = req.getParameter("name");
String age = req.getParameter("age");
String email = req.getParameter("email");
User user = new User(name, new Integer(age), email);
sendMail mail = new sendMail(user);
mail.start();
req.setAttribute("meg","您已注册成功,待会您将会收到一封注册成功的邮件");
req.getRequestDispatcher("info.jsp").forward(req,resp);
}
}
消息页面
<%--
Created by IntelliJ IDEA.
User: 11751
Date: 2022-4-23 , 0023
Time: 16:14
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>注册成功</title>
</head>
<body>
${meg}
</body>
</html>
配置
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0"
metadata-complete="true">
<servlet>
<servlet-name>doregister</servlet-name>
<servlet-class>com.nn.servlet.doRegister</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>doregister</servlet-name>
<url-pattern>/doregister</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>register.jsp</welcome-file>
</welcome-file-list>
</web-app>
<dependencies>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
<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>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
</dependencies>
获取邮箱序列号
|