邮件发送servelet实现
package com.xbs.register.main;
import java.io.IOException;
import java.util.Date;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.Transport;
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 javax.servlet.http.HttpSession;
/**
* @author James
* 发送邮件的servlet实现
*/
public class RegisterServlet extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
this.doPost(req, resp);
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/html");
// 编码
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
String toMail = req.getParameter("email");
String registerName = req.getParameter("userName");
String userName = "[email protected]";
String password = "wjw6993469";
String registerId = "" + Math.random() * Math.random();
String url = "http://localhost:8080/register/MailBackServlet?registerId="
+ registerId+"®isterName="+registerName;// 待会用户点在邮箱中点击这个链接回到你的网站。
HttpSession httpSession = req.getSession();
httpSession.setAttribute(registerId, registerName);
// 秒数
httpSession.setMaxInactiveInterval(1000);
httpSession.setAttribute("2", registerName);
/*
* System.out.println("------"+registerId); String str = (String)
* httpSession.getAttribute(registerId);
* System.out.println("----------------"+str);
* System.out.println(registerName);
*/
// 设置身份验证、服务器等信息
Properties props = new Properties();
props.setProperty("mail.smtp.host", "smtp.qq.com");
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.store.protocol", "smtp");
Authenticator authenticator = new MyAuthenticator(userName, password);
javax.mail.Session session = javax.mail.Session.getDefaultInstance(
props, authenticator);
session.setDebug(true);
try {
Address from = new InternetAddress(userName);
Address to = new InternetAddress(toMail);
MimeMessage msg = new MimeMessage(session);
msg.setFrom(from);
msg.setSubject("诚实网站注册");
msg.setSentDate(new Date());
msg.setContent("<a href=‘" + url + "‘>点击" + url + "完成注册</a>",
"text/html;charset=utf-8");
msg.setRecipient(RecipientType.TO, to);
/*
* Transport transport = session.getTransport("smtp");
* transport.connect("smtp.163.com", userName, password);
* transport.sendMessage(msg,msg.getAllRecipients());
* transport.close();
*/
Transport.send(msg);
} catch (MessagingException e) {
e.printStackTrace();
}
req.getRequestDispatcher("/sendMailSuccess.jsp").forward(req, resp);
}
}
/**
* 发送邮件的邮箱的授权用户名和密码类
*/
package com.xbs.register.main;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
/**
* @author James
*
*/
public class MyAuthenticator extends Authenticator {
private String userName;
private String password;
public MyAuthenticator(String userName, String password){
this.userName = userName;
this.password = password;
}
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, password);
}
}
简单的注册页面
register.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>用户注册利用邮箱进行验证</title>
</head>
<body>
<br/>
<form action="${pageContext.request.contextPath }/RegisterServlet" method="post">
<font size="24" color="red">用户注册</font><br/>
邮箱:<input type="text" name="email" /><br/>
昵称:<input type="text" name="userName" /><br/>
<input type="submit" value="submit" /><br/>
</form>
</body>
</html>
所需要的jar报。mail.jar和servlet.jar
http://www.2cto.com/kf/201312/268157.html例子