Web发送新邮件
WEB email发送
util
SendMailThread
package com.dgxan.util;
import com.dgxan.pojo.User;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
public class SendMailThread extends Thread {
//发件人
private String form = "18035114874@163.com";
//收件人
private String receiver = "18035114874@163.com";
//邮箱用户名
private String userName = "18035114874@163.com";
//邮箱的授权码
private String userPwd = "DZKVHRBZQVGJVEAN";
//邮箱服务器
private String host = "smtp.163.com";
private User user;
public SendMailThread(User user){
this.user = user;
}
@Override
public void run() {
//配置文件
Properties prop = new Properties();
prop.setProperty("mail.host","smtp.163.com");
prop.setProperty("mail.transport.protocol","smtp");
prop.setProperty("mail.smtp.auth","true");
//发送邮件的五个步骤
/*
* 第一步: 创建Session对象
* 第二步: 通过SessIon得到transport对象
* 第三步: 使用邮箱的用户名和授权码连上邮件服务器
* 第四步: 创建邮件
* 第五步: 发送邮件
* */
Session session = Session.getDefaultInstance(prop, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, userPwd);
}
});
session.setDebug(true);
Transport ts = null;
try {
ts = session.getTransport();
ts.connect(host,userName,userPwd);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(userName));
message.setSubject("注册邮箱");
message.setRecipient(Message.RecipientType.TO,new InternetAddress(receiver));
message.setContent("<h1 style='color:red'>你好,您的密码是"+user.getPassword()+"</h1>","text/html;charset=UTF-8");
ts.sendMessage(message, message.getAllRecipients());
}catch (Exception e){
e.printStackTrace();
}finally {
try {
ts.close();
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
}
SendMailServlet
package com.dgxan.servlet;
import com.dgxan.pojo.User;
import com.dgxan.util.SendMailThread;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
public class SendMailServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String name = req.getParameter("name");
String pwd = req.getParameter("pwd");
String mail = req.getParameter("mail");
User user = new User(name,pwd,mail);
SendMailThread thread = null;
HttpSession session = req.getSession();
if (user!=null){
thread = new SendMailThread(user);
thread.start();
session.setAttribute("message","正在注册中,请稍候");
}else {
session.setAttribute("message","注册失败");
}
req.getRequestDispatcher("/info.jsp").forward(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req,resp);
}
}
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<form method="post" action="${pageContext.request.contextPath}/mail.do">
<table>
<tr>
<td>姓名:</td>
<td><input type="text" name="name"/></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="text" name="pwd"/></td>
</tr>
<tr>
<td>E-Mail:</td>
<td><input type="email" name="mail"/></td>
</tr>
<tr>
<td><input type="submit" value="注册"/></td>
</tr>
</table>
</form>
</body>
</html>
info.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Info</title>
</head>
<body>
${sessionScope.message}
<a href="index.jsp">请返回注册界面</a>
</body>
</html>
User
package com.dgxan.pojo;
public class User {
private String name;
private String password;
private String email;
public User() {
}
public User(String name, String password, String email) {
this.name = name;
this.password = password;
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", password='" + password + '\'' +
", email='" + email + '\'' +
'}';
}
}
人生不易,多多努力!!!
浙公网安备 33010602011771号