最近学习用JSP做通过邮箱找回密码的方法
看了很多资料 也写了很多代码
但一直出错 后来还是一位朋友和我一起做出来了
现在共享给大家 希望对大家学习有帮助
<%@ page contentType="text/html;charset=gb2312" %>
<%@ page import="java.util.*,javax.mail.*"%>
<%@ page import="javax.mail.internet.*"%>
<%@ page import="javax.activation.*"%>
<%@ page import="java.net.*"%>
<jsp:useBean id="userInfo" class="www.UserInfo" scope="page">
<jsp:setProperty name="userInfo" property="*"/>
</jsp:useBean>
<jsp:useBean id="MD5" class="www.MD5" scope="session"/>
<jsp:useBean id="regist" class="www.UserRegist" scope="page"/>
<%!
public String codeToString(String str)
{ //处理中文字符集的函数
String s=str;
try{
byte tempB[]=s.getBytes("ISO-8859-1");
s=new String(tempB);
return s;
}
catch(Exception e)
{
return s;
}
}
%>
<html>
<body>
<%
regist.setUserInfo(userInfo);
String email = request.getParameter("email");
String yanzhengma = request.getParameter("yanzhengma");
String tupianyanzhengma = session.getAttribute("rand").toString();
if(yanzhengma.equals(tupianyanzhengma))
{
regist.regist();
}
else
{
response.sendRedirect("register.jsp?email="+email);
}
try{
String to_mail=codeToString(request.getParameter("email"));
String to_title="标题";
String to_content=" 内容 <a href='http://192.168.1.115:8888/Mzjkw3/mzjkwqt/active.jsp?jihuo="+ to_mail +"'>激活</a> <a href='http://192.168.1.115:8888/Mzjkw3/mzjkwqt/active1.jsp?jihuo="+ to_mail +"'>取消激活</a>";
Properties props=new Properties();
props.put("mail.smtp.host","smtp.163.com");
props.put("mail.smtp.auth","true");
Session s=Session.getInstance(props);
s.setDebug(true);
MimeMessage message=new MimeMessage(s);
//给消息对象设置发件人、收件人、主题、发信时间
InternetAddress from=new InternetAddress("liumin1182005@163.com");
message.setFrom(from);
InternetAddress to =new InternetAddress(to_mail);
message.setRecipient(Message.RecipientType.TO,to);
message.setSubject(to_title);
message.setSentDate(new Date());
Multipart mm=new MimeMultipart();//新建一个MimeMultipart对象用来存放多个BodyPart对象
//设置信件文本内容
BodyPart mdp=new MimeBodyPart();//新建一个存放信件内容的BodyPart对象
mdp.setContent(to_content,"text/html;charset=gb2312");//给BodyPart对象设置内容和格式/编码方式
mm.addBodyPart(mdp);//将含有信件的内容的BodyPart加入到MimeMultipart对象中
message.setContent(mm);//把mm作为消息对象的内容
message.saveChanges();
Transport transport=s.getTransport("smtp");
//以smtp方式登录邮箱,第一个参数是发送邮件用的邮件服务器SMTP地址,第二个参数为用户名,第三个参数为密码
transport.connect("smtp.163.com","你的163油箱","密码");
transport.sendMessage(message,message.getAllRecipients());
transport.close();
}catch(MessagingException e){
out.println(e.toString());
}
%>
<br><br>
<a href="index.jsp">注册成功!<br>请您注意查收激活邮件,我们将在24小时内发送激活邮件到您的注册信箱!点击此处返回主页面!</a>
</body>
</html>
写此代码时 用了mail.jar和activation.jar 两个包
下载他们然后加入环境变量
后者扶植到/jre/lib/ext/目录下