javamail忘记密码功能
4. JavaMail发送邮件三大步骤
4.1. 创建会话(javax.mail.Session)
Properties props = new Properties();
props.put("mail.smtp.host", smtpHost); //设置smtp主机名
props.put("mail.smtp.auth", "true"); //需要安全验证
//创建网络连接身份验证类对象
MailAuthenticator mailauth = new MailAuthenticator(userName, password);
Session sendMailSession = Session.getDefaultInstance(props, mailauth);
session.setDebug(true); //启动调试
props.put("mail.smtp.host", smtpHost); //设置smtp主机名
props.put("mail.smtp.auth", "true"); //需要安全验证
//创建网络连接身份验证类对象
MailAuthenticator mailauth = new MailAuthenticator(userName, password);
Session sendMailSession = Session.getDefaultInstance(props, mailauth);
session.setDebug(true); //启动调试
4.2. 编写消息(javax.mail.Message)
// 创建message对象
Message msg = new MimeMessage(sendMailSession);
// 设置发件人
msg.setFrom(new InternetAddress(from));
// 设置主收件人
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
// 同时可设置多个抄送或暗送
//msg.setRecipients(Message.RecipientType.CC, new InternetAddress[]{…});
// 设置邮件标题
msg.setSubject(subject);
// 设置邮件内容(纯文本格式)
msg.setText(content); //msg.setContent(content, "text/plain")
// 设置邮件发送时间
msg.setSentDate(new java.util.Date());
Message msg = new MimeMessage(sendMailSession);
// 设置发件人
msg.setFrom(new InternetAddress(from));
// 设置主收件人
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
// 同时可设置多个抄送或暗送
//msg.setRecipients(Message.RecipientType.CC, new InternetAddress[]{…});
// 设置邮件标题
msg.setSubject(subject);
// 设置邮件内容(纯文本格式)
msg.setText(content); //msg.setContent(content, "text/plain")
// 设置邮件发送时间
msg.setSentDate(new java.util.Date());
4.3. 发送消息(javax.mail.Transport)
// 利用Transpost即可发送邮件
Transport.send(msg);
5. 使用JavaMail模仿CSDN找回密码的功能
5.1. 写个Mail类,包含邮件所需各参数及发件功能
Mail.java:
- /**
- * ClassName: Mail.java
- * Created on 2011
- * Copyrights 2011 hi.csdn.net/tjcyjd All rights reserved.
- * site: http://hi.csdn.net/tjcyjd
- * email: 908599713@qq.com
- */
- package com.tjcyjd.web.mail;
- import java.util.Date;
- import java.util.Properties;
- import javax.mail.Authenticator;
- import javax.mail.Message;
- import javax.mail.Multipart;
- import javax.mail.PasswordAuthentication;
- import javax.mail.Session;
- import javax.mail.Transport;
- import javax.mail.internet.MimeBodyPart;
- import javax.mail.internet.MimeMessage;
- import javax.mail.internet.MimeMultipart;
- import javax.mail.internet.MimeUtility;
- import javax.mail.internet.InternetAddress;
- /**
- * Mail实体类(包含发件功能)
- *
- * @author yjd
- */
- public class Mail {
- String to = ""; // 收件人
- String from = ""; // 发件人
- String host = ""; // smtp主机
- String username = ""; // 用户名
- String password = ""; // 密码
- String subject = ""; // 邮件主题
- String content = ""; // 邮件正文
- public Mail() {
- }
- public Mail(String to, String from, String host, String username,
- String password, String subject, String content) {
- this.to = to;
- this.from = from;
- this.host = host;
- this.username = username;
- this.password = password;
- this.subject = subject;
- this.content = content;
- }
- /**
- * 把主题转换为中文
- *
- * @param strText
- * @return
- */
- public String transferChinese(String strText) {
- try {
- strText = MimeUtility.encodeText(new String(strText.getBytes(),
- "GB2312"), "GB2312", "B");
- } catch (Exception e) {
- e.printStackTrace();
- }
- return strText;
- }
- /**
- * 发送邮件
- *
- * @return 成功返回true,失败返回false
- */
- public boolean sendMail() {
- // 构造mail session
- Properties props = System.getProperties();
- props.put("mail.smtp.host", host);
- props.put("mail.smtp.auth", "true");
- Session session = Session.getDefaultInstance(props,
- new Authenticator() {
- public PasswordAuthentication getPasswordAuthentication() {
- return new PasswordAuthentication(username, password);
- }
- });
- try {
- // 构造MimeMessage并设定基本的值,创建消息对象
- MimeMessage msg = new MimeMessage(session);
- // 设置消息内容
- msg.setFrom(new InternetAddress(from));
- System.out.println(from);
- // 把邮件地址映射到Internet地址上
- InternetAddress[] address = { new InternetAddress(to) };
- /**
- * setRecipient(Message.RecipientType type, Address
- * address),用于设置邮件的接收者。<br>
- * 有两个参数,第一个参数是接收者的类型,第二个参数是接收者。<br>
- * 接收者类型可以是Message.RecipientType .TO,Message
- * .RecipientType.CC和Message.RecipientType.BCC,TO表示主要接收人,CC表示抄送人
- * ,BCC表示秘密抄送人。接收者与发送者一样,通常使用InternetAddress的对象。
- */
- msg.setRecipients(Message.RecipientType.TO, address);
- // 设置邮件的标题
- subject = transferChinese(subject);
- msg.setSubject(subject);
- // 构造Multipart
- Multipart mp = new MimeMultipart();
- // 向Multipart添加正文
- MimeBodyPart mbpContent = new MimeBodyPart();
- // 设置邮件内容(纯文本格式)
- // mbpContent.setText(content);
- // 设置邮件内容(HTML格式)
- mbpContent.setContent(content, "text/html;charset=utf-8");
- // 向MimeMessage添加(Multipart代表正文)
- mp.addBodyPart(mbpContent);
- // 向Multipart添加MimeMessage
- msg.setContent(mp);
- // 设置邮件发送的时间。
- msg.setSentDate(new Date());
- // 发送邮件
- Transport.send(msg);
- } catch (Exception e) {
- e.printStackTrace();
- return false;
- }
- return true;
- }
- public String getTo() {
- return to;
- }
- public void setTo(String to) {
- this.to = to;
- }
- public String getFrom() {
- return from;
- }
- public void setFrom(String from) {
- this.from = from;
- }
- public String getHost() {
- return host;
- }
- public void setHost(String host) {
- this.host = host;
- }
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- public String getSubject() {
- return subject;
- }
- public void setSubject(String subject) {
- this.subject = subject;
- }
- public String getContent() {
- return content;
- }
- public void setContent(String content) {
- this.content = content;
- }
- }
5.2. 写个找回密码的Action类
MemberAction.java:
- /**
- * ClassName: MemberAction.java
- * Created on 2011
- * Copyrights 2011 hi.csdn.net/tjcyjd All rights reserved.
- * site: http://hi.csdn.net/tjcyjd
- * email: 908599713@qq.com
- */
- package com.tjitcast.bbs.web.action.front;
- import org.apache.struts2.convention.annotation.Action;
- import org.apache.struts2.convention.annotation.Namespace;
- import org.apache.struts2.convention.annotation.ParentPackage;
- import org.springframework.context.annotation.Scope;
- import org.springframework.stereotype.Controller;
- import com.tjitcast.bbs.domain.Member;
- import com.tjitcast.web.action.common.BaseAction;
- import com.tjitcast.web.mail.Mail;
- /**
- * 会员找回密码Action类
- *
- * @author yjd
- */
- @Controller("front.memberAction")
- @Scope("prototype")
- @Namespace("/")
- @ParentPackage("struts-default")
- public class MemberAction extends BaseAction {
- private static final long serialVersionUID = -4631080996625964833L;
- private Member member;
- /**
- * 找回密码
- */
- @Action(value = "findpassword")
- public void findPassWord() {
- Member temp = memberService.get(member.getLoginname());
- if (temp != null) {
- String toMail = temp.getEmail();
- // String basePath = this.getBasePath();
- StringBuffer sb = new StringBuffer();
- sb.append("亲爱的用户 tjcyjd:您好!<br><br>");
- sb
- .append(" 您收到这封这封电子邮件是因为您 (也可能是某人冒充您的名义) 申请了一个新的密码。假如这不是您本人所申请, 请不用理会<br>这封电子邮件, 但是如果您持续收到这类的信件骚扰, 请您尽快联络管理员。<br><br>");
- sb
- .append(" 要使用新的密码, 请使用以下链接启用密码。<br><br>");
- sb
- .append(" <a href='http://passport.csdn.net/account/resetpassword?user=tjcyjd&active=jJTi9HgBmARmyittIJ7fBvzCtbvaz6FCXj0ZXJpn940=0'>http://passport.csdn.net/account/resetpassword?user=tjcyjd&active=jJTi9HgBmARmyittIJ7fBvzCtbvaz6FCXj0ZXJpn940=0</a>");
- sb
- .append("<br><br><br>关注好友动态,请访问CSDN个人空间:<a href='http://hi.csdn.net'>http://hi.csdn.net</a><br>");
- sb
- .append("修改个人档案资料,请访问CSDN个人设置:<a href='http://passport.csdn.net/account/profile'>http://passport.csdn.net/account/profile</a><br>");
- sb
- .append("技术精品文章,请访问CSDN博客:<a href='http://blog.csdn.net/'>http://blog.csdn.net/</a><br>");
- sb
- .append("全球最大的中文技术讨论区,请访问CSDN论坛:<a href='http://community.csdn.net/'>http://community.csdn.net/</a><br>");
- sb
- .append("分享您认为最好的内容,请访问CSDN下载频道:<a href='http://download.csdn.net/'>http://download.csdn.net/</a><br>");
- sb
- .append("业界新闻、人才、外包 等等更多CSDN服务,请访问<a href='http://www.csdn.net/'>http://www.csdn.net/</a><br>");
- sb.append("<br><br>我们将一如既往、热忱的为您服务!");
- sb
- .append("<br><br>WWW.CSDN.NET - 中国最大的IT技术社区,为IT专业技术人员提供最全面的信息传播和服务平台");
- sb
- .append("<br>用户服务支持:<a href='mailto:webmaster@csdn.net'>webmaster@csdn.net</a><br><br><br>");
- /** strm[1]第一个跟第二个@间内容,strm[strm.length - 1]最后一@内容 */
- String strm[] = toMail.split("@");
- // 创建邮件
- Mail mail = new Mail();
- mail.setTo(toMail);
- mail.setFrom("y20110506@163.com");// 你的邮箱
- mail.setHost("smtp.163.com");
- mail.setUsername("y20110506@163.com");// 用户
- mail.setPassword("******");// 密码
- mail.setSubject("[CSDN]找回您的账户密码");
- mail.setContent(sb.toString());
- if (mail.sendMail()) {
- renderText("您的申请已提交成功,请查看您的******" + strm[strm.length - 1]
- + "邮箱。");
- } else {
- renderText("操作失败,轻稍后重新尝试!");
- }
- } else {
- renderText("当前的用户名不存在!");
- }
- }
- public Member getMember() {
- return member;
- }
- public void setMember(Member member) {
- this.member = member;
- }
- }
5.3. Jsp页面访问
find_password.jsp:
- <%@ page pageEncoding="UTF-8"%>
- <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
- <%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">