导入依赖
<groupId>com.zou</groupId>
<artifactId>WebMail</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.10</version>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
</project>
建立实体类发送
package com.zou.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User implements Serializable {
private String username;
private String password;
private String email;
}
建立工具类发送邮件
package com.zou.util;
import com.zou.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 ="2246781190@qq.com";
private String username ="2246781190@qq.com";
private String password ="udgyxytarwlgecae";
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.qq.com");//设置请求邮件服务器
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() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("2246781190@qq.com", "utrwlgecae");
}
});
session.setDebug(true);
//通过session得到transport对象
Transport transport = session.getTransport();
transport.connect("smtp.qq.com", "2246781190@qq.com", "uytrwlgecae");
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("2246781190@qq.com"));
message.setRecipient(Message.RecipientType.TO, new InternetAddress(user.getEmail()));
message.setSubject("用户注册事件");
String info="恭喜您注册成功,您的用户名:"+user.getUsername()+",您的密码;"+user.getPassword()+",请妥善你保管,如有问题请联系客服";
message.setContent(info,"text/html;charset=UTF-8");
message.saveChanges();
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}catch (Exception e){
throw new RuntimeException(e);
}
}
}
建立jsp主页
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>注册</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/RegisterServlet.do" method="post">
用户名:<input type="text" name="username"><br/>
密码:<input type="password" name="password"><br/>
邮箱:<input type="text" name="email"><br/>
<input type="submit" value="注册">
</form>
</body>
</html>
</body>
</html>
发送后跳转jsp页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>
网站提示
</h1>
${message}
</body>
</html>
跳转内容
package com.zou.servlet;
import com.zou.pojo.User;
import com.zou.util.Sendmail;
import javax.servlet.ServletException;
import java.io.IOException;
public class RegisterServlet extends javax.servlet.http.HttpServlet{
protected void doPost(javax.servlet.http.HttpServletRequest request,javax.servlet.http.HttpServletResponse response)throws ServletException,IOException {
String username =request.getParameter("username");
String password =request.getParameter("password");
String email =request.getParameter("email");
User user = new User(username,password,email);
Sendmail send=new Sendmail(user);
send.start();
request.setAttribute("message","注册成功,我们已经发了一封电子邮件,如网络不稳定,可能过一会才能收到!!");
request.getRequestDispatcher("info.jsp").forward(request,response);
}
protected void doGet(javax.servlet.http.HttpServletRequest request,javax.servlet.http.HttpServletResponse response)throws ServletException,IOException{
}
}