通过发送邮件来找回密码

警告: javamail包与 geronimo-javamail_1.4_spec-1.3.jar 它有冲突,
类似这种冲突的包有好多,大家要千万注意,否则会让你纠结许久。此工程我使用 javaMail包

开始正题:
1:在工程下创建 发送邮件模板,此处我采取的是 用velocity 的方式,所以后缀名为 .vm
<html>
<body>
<div id="contentDiv" style="font-size:14px;height:auto;padding:15px 0 10px 15px;*padding:15px 0 0 15px;overflow:visible;line-height:170%;min-height:100px;_height:100px;" class="" >
	<table id="" width="100%" cellpadding="0" cellspacing="0" border="0">
		<tr>
			<td valign="top" style="font-size:14px;padding:0;height:auto;min-height:auto;font-family: 'lucida Grande',Verdana;" class="body">
				<div id="mailContentContainer" class="left">
                    <style type='text/css'>
                     <!--
                     body,td,th {
                     	font-size: 12px;
                    	color: #393939;
                    	line-height: 24px;
                     }
                     .te2 {WIDTH: 80px;	HEIGHT: 17px;BACKGROUND-COLOR: #ffffff;border: 1px solid #A6BEE0;}
                     .font_line28 {line-height: 32px; ; ;}
                     .font_blue14_bold {color:#1369BF;font-weight: bold;	font-size: 14px;line-height: 23px;}
                     .font_blue122_bold {color:#4D728F;font-weight: bold;;}
                     .font_red12 { font-size:12px;color:#FF0000;}
                     .fant_hui12 { font-size:12px;color:#B9B9B9;}
                     .font_blue12{color:#4D728F;	padding-right: 7px;}
                     .font_blue12 a:link{ color:#4D728F; text-decoration:underline;}
                     .font_blue12 a:visited{color:#4D728F; text-decoration:underline;}
                    .font_blue12 a:hover {color:#4D728F; text-decoration:underline;}
                    -->
                    </style>
                        <table width='500' border='0' align='center' cellpadding='0' cellspacing='1' bgcolor='#0BA5EB'>
                          <tr>
                            <td align='left' valign='top' bgcolor='#FFFFFF'>
                        		<table width='100%' border='0' cellspacing='0' cellpadding='0'>
                                  <tr>
                                    <td width='39' height='26' background='http://uc.cun365.com/images/cun365/reg_r2_c4.jpg'><img src='http://10.10.1.11/ucenter/images/cun365/reg_r2_c21.jpg' width='34' height='26'/></td>
                                    <td width='577' align='left' valign='bottom' background='http://uc.cun365.com/images/cun365/reg_r2_c4.jpg' class='font_blue14_bold'>确认邮件</td>
                                    <td width='32' align='left' valign='middle' background='http://uc.cun365.com/images/cun365/reg_r2_c4.jpg'><img src='http://uc.cun365.com/images/cun365/gif-0081.gif' width='23' height='17'/></td>
                                  </tr>
                                </table>
                        	</td>
                          </tr>
                          <tr>
                            <td align='center' valign='top' bgcolor='#FFFFFF'>
                        		<table width='92%' border='0' cellspacing='0' cellpadding='0'>
                                  <tr>
                                    <td height='12'></td>
                                  </tr>
                                  <tr>
                                    <td align='left' valign='top'>
                            			亲爱的<span class='font_red12'>$!{userName}</span>,您好:<br/>
                            &nbsp;&nbsp;&nbsp; 当您收到这封信的时候,您已经可以用新密码正常登录了。<br/>
                            &nbsp;&nbsp;&nbsp; 您的新密码为:&nbsp;&nbsp;&nbsp;<span class='font_red12'>
                            $!{password}</span>
                            		</td>
                                  </tr>
                                  <tr>
                                    <td height='13' align='left' valign='top'></td>
                                  </tr>
                                  <tr>
                                    <td height='13' align='left' valign='top' background='http://uc.cun365.com/images/cun365/reg_line.jpg'></td>
                                  </tr>
                                  <tr>
                                    <td align='left' valign='top'>
										<span class='font_blue122_bold'>点击以下链接,访问我xxx:</span><br/>
                                       <a href='http://www.xxx.com' target="_blank" class='font_blue12'>http://www.cun365.com</a>
									   <span class='fant_hui12'><br/>(如果不能点击该链接地址,请复制并粘贴到浏览器的地址输入框)</span>
									</td>
                                  </tr>
                                  <tr>
                                    <td align='left' valign='top'>
										<span class='font_blue122_bold'>点击以下链接,进行修改密码:</span><br/>
                                       <a href='http://uc.cun365.com/login?service=http://xx.com/xx?oldPwd=$!{password}' target="_blank" class='font_blue12'>http://uc.cun365.com/login?service=http://xx.com/?oldPwd=$!{password}</a>
									   <span class='fant_hui12'><br/>(如果不能点击该链接地址,请复制并粘贴到浏览器的地址输入框)</span>
									</td>
                                  </tr>                                    
                                  <tr>
                                    <td height='40' align='center' valign='middle'><span class='font_gray14'>
                                      <a href='http://xxx?service=http://xxx?json=abcdeu123' target='_blank'><img src='http://uc.cun365.com/images/cun365/but-1.gif' width='203' height='24' border=0//></a>
                                    </span></td>
                                  </tr>
                                  
                                  <tr>
                                    <td height='13' align='left' valign='top'></td>
                                  </tr>
                                  <tr>
                                    <td height='13' align='left' valign='top' background='http://uc.cun365.com/images/cun365/reg_line.jpg'></td>
                                  </tr>
                                  <tr>
                                    <td align='left' valign='top'>
                            			<p> 此邮件为系统所发,请勿直接回复。&nbsp; &nbsp; <br/><br/></p>
                                    </td>
                                  </tr>
                                  <tr>
                                    <td align='left' valign='top'>&nbsp;</td>
                                  </tr>
                                </table>
                            </td>
                          </tr>
                        </table>
				</div>
			</td>
			<td width="170">&nbsp;</td>
		</tr>
	</table>
</div>
<div id="mailContentContainer">
&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;如果您有任何问题,请查看<a href="#">隐私权保护规则</a><br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;服务热线:15034401440   电子邮箱:<a href="mailto:[email protected]">[email protected]</a>
</div>
</body>
</html>


2:写认证类(因为我采取的是国际标准,所以比较繁琐,当然也有简单的方法,大家在开发中还是以标准为好)
package com.transnal.utils;

import javax.mail.*;   

/**
 * Smtp认证,此类事必须要有的,是为了遵循sum公司定下的规范
 * @author renwg
 * @since 2010.5.30
 * @version 1.0
 *
 */
public class MyAuthenticator extends Authenticator{   
    private String userName=null;   
    private String password=null;   
        

	public MyAuthenticator(){}   
    
    /**  
     * SMTP身份验证  
     * @param username  
     * @param password  
     */    
    public MyAuthenticator(String username, String password) {    
        this.userName = username;    
        this.password = password;    
    }    
    protected PasswordAuthentication getPasswordAuthentication(){  
    	System.out.println("服务器正在验证用户:"+userName);
    	System.out.println("服务器正在验证密码:"+password);
        return new PasswordAuthentication(userName, password);  
    }   
}   




以下是一个公共类,此类中包含企业常用的方法
package com.transnal.utils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.ResourceBundle;
import java.util.regex.Pattern;

import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMultipart;
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sxinfo.profile.Profile;
import net.sxinfo.profile.ProfileInfo;
import net.sxinfo.user.User;
import net.sxinfo.user.UserFactory;  //这个东西我不说,你也知道了,就是一个读取 资源文件的 类。

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;

import com.opensymphony.xwork2.ActionContext;

/**
 * 所有共用的方法都被调到这个类里
 * 
 * @author RenWeigang
 * 
 * @since 2010.08.02
 * 
 * @version 1.0
 *
 */
public class WebUtil {
	
	private static String directoryId;
	private static String userName;
	
	
	/**
	 * 設置提示信息(僅限request請求輸出)
	 * @param paramString
	 */
	public static void addError(String paramString){
		ServletActionContext.getRequest().setAttribute("msg",paramString);
	}
	
	/**
	 * URI跳转
	 */
	public static void redirectService(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String s_m = request.getParameter("success_msg");
		if(StringUtils.isNotBlank(s_m) && (s_m.equals("%E6%88%90%E5%8A%9F") || s_m.equals("成功"))){
			HttpServletResponse response = ServletActionContext.getResponse();
			StringBuilder sb = new StringBuilder("修改成功!");
			String paramService = request.getParameter("service");
			if(StringUtils.isNotBlank(paramService) && (paramService.startsWith("http") || paramService.startsWith("ftp"))){
				if(paramService.startsWith("http"))
				{
					paramService ="http://"+paramService.replaceAll("^http\\:\\/+","");
				}
				if(paramService.startsWith("ftp")){
					paramService ="ftp://"+paramService.replaceAll("^ftp\\:\\/+","");
				}
				sb.append("3秒后跳转到您原始的页面!如没有相应,请点击<font color='red'><a href='");
				sb.append(paramService);
				sb.append("' target='_blank'>");
				sb.append("这里");
				sb.append("</a>");
				sb.append("</font>");
				String url = getBasePath(request)+"logout?url="+paramService;
				response.setHeader("Refresh","3;URL="+url);
			}
			ServletActionContext.getRequest().setAttribute("msg",sb.toString());	
		}
	}
	
	/**
	 * 发送密码到邮箱
	 * @param user
	 * 			用户对象
	 * @param password
	 * 			新密码
	 * @param email
	 * 			收密码的邮箱号码
	 */
	public static void sendEmail(User user,String password,String email){
		try {
			InputStream in = UserFactory.class.getResourceAsStream("/spring-jpa.properties");
			Properties p = new Properties();
			if (in != null) {
				p.load(in);
			}
			String authuser=p.getProperty("mail.username");  //发送人用户名
			String authpwd= p.getProperty("mail.password");  //邮箱密码
			String host = p.getProperty("mail.host");        //smtp服务器名字
			String port = p.getProperty("mail.port");		 //端口号
			String from = p.getProperty("mail.from");        //来自哪里
			String title = p.getProperty("ucenter.title");   //邮件标题,替换发送的邮箱名字
			String url = p.getProperty("ucenter.url");       //链接地址
			
			Properties sessionPro = new Properties();
			sessionPro.put("mail.smtp.host", host);
			sessionPro.put("mail.smtp.port",port);
			sessionPro.put("mail.smtp.auth","true");
			
			// 根据邮件会话属性和密码验证器构造一个发送邮件的session 
			Session sendMailSession = Session.getDefaultInstance(sessionPro,new MyAuthenticator(authuser,authpwd));
			
			// 根据session创建一个邮件消息    
			Message mailMessage = new MimeMessage(sendMailSession);
			
			// 创建邮件发送者地址    
			Address fromAddress = new InternetAddress(from);
			
			// 创建邮件的接收者地址,并设置到邮件消息中    
			Address toAddress = new InternetAddress(email);
			
			// 设置邮件消息的发送者 
			mailMessage.setFrom(fromAddress);
			
			mailMessage.setFileName(title);
			
			mailMessage.setRecipient(Message.RecipientType.TO,toAddress);
			// 设置邮件消息发送的时间    
			mailMessage.setSentDate(new Date()); 
			mailMessage.setSubject("找回密码");
			
			// MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象    
			Multipart mainPart = new MimeMultipart();
			// 创建一个包含HTML内容的MimeBodyPart    
			BodyPart html = new MimeBodyPart();
			
			//设置邮件内容
			Map<String,String> props=new HashMap<String,String>();
			props.put("title", title);
			props.put("url", url);
			props.put("userName",user.getUserName());
			props.put("password",password);
			
			//读取邮件内容
			String result=emailTemplate(props,"mail/findpwd_mail.vm");;
            
			html.setContent(result,"text/html;charset=utf-8");
			mainPart.addBodyPart(html);
			
			 // 将MiniMultipart对象设置为邮件内容    
			mailMessage.setContent(mainPart);
			
     		//保存要发送的邮件
		    mailMessage.saveChanges();
            
			//发送邮件
			Transport.send(mailMessage);
			
			StringBuilder sb = new StringBuilder();
			sb.append("系统已经把密码发送到您的邮箱,请点击");
			sb.append("<font color='red'><a target='_blank' href='http://mail.");
			sb.append(email.substring(email.indexOf("@")+1,email.length())+"'");
			sb.append(">");
			sb.append("这里登录邮箱");
			sb.append("</a></font>");
			sb.append("查收!");
			System.out.println(sb.toString());
			addError(sb.toString());
		} catch (Exception ex) {
			addError("网络连接失败!检查您的网络是否正常!");
		}
				
	}
	
	/**
	 * 读取邮件模板
	 * @param data
	 * @return
	 */
	private static String emailTemplate(Map<String, String> data,String filePath) {
		String result = null;
		try {
			VelocityEngine velocity = new VelocityEngine();
			Properties properties = new Properties();
			properties.setProperty("resource.loader", "class");
			properties.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
			velocity.init(properties);
			Template t = velocity.getTemplate(filePath, "utf-8");
			VelocityContext context = new VelocityContext();
			for (Map.Entry<String, String> entry : data.entrySet()) {
				context.put(entry.getKey(), entry.getValue());
			}
			StringWriter writer = new StringWriter();
			t.merge(context, writer);
			result = writer.toString();
			writer.close();
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		return result;
	}	
		

	/**
	 * 检查验证码
	 * @param verifycodeBeValid 
	 * 			验证码是否正确
	 * @param verifycode
	 * 			验证码
	 * @return
	 */
	public static boolean checkVerifycode(String verifycode) {
		boolean verifycodeBeValid = false;
		String rand_image = null;
		try {
			rand_image = (String) ActionContext.getContext().getSession().get("rand_image");
			if (verifycode.equals(rand_image)) {
				verifycodeBeValid = true;
			}
		} catch (Exception ex) {
			verifycodeBeValid = false;
		}
		return verifycodeBeValid;
	}	
	
	 
    /***
     * 获取URI的路径,如路径为http://www.cun365.com/passport/center.action?ssid=1, 得到的值为"/passport.center.action"
     * @param request
     * @return
     */
    public static String getRequestURI(HttpServletRequest request){     
        return request.getRequestURI();
    }
    /**
     * 获取完整请求路径(含内容路径及请求参数)
     * @param request
     * @return
     */
    public static String getRequestURIWithParam(HttpServletRequest request){     
        return getRequestURI(request) + (request.getQueryString() == null ? "" : "?"+ request.getQueryString());
    }
    
    /**
     * 添加cookie
     * @param response
     * @param name cookie的名称
     * @param value cookie的值
     * @param maxAge cookie存放的时间(以秒为单位,假如存放三天,即3*24*60*60; 如果值为0,cookie将随浏览器关闭而清除)
     */
    public static void addCookie(HttpServletResponse response, String name, String value, int maxAge) {        
        Cookie cookie = new Cookie(name, value);
        cookie.setPath("/");
        if (maxAge>0) cookie.setMaxAge(maxAge);
        response.addCookie(cookie);
    }
    
    /**
     * 获取cookie的值
     * @param request
     * @param name cookie的名称
     * @return
     */
    public static String getCookieByName(HttpServletRequest request, String name) {
    	Map<String, Cookie> cookieMap = WebUtil.readCookieMap(request);
        if(cookieMap.containsKey(name)){
            Cookie cookie = (Cookie)cookieMap.get(name);
            return cookie.getValue();
        }else{
            return null;
        }
    }
    
    protected static Map<String, Cookie> readCookieMap(HttpServletRequest request) {
        Map<String, Cookie> cookieMap = new HashMap<String, Cookie>();
        Cookie[] cookies = request.getCookies();
        if (null != cookies) {
            for (int i = 0; i < cookies.length; i++) {
                cookieMap.put(cookies[i].getName(), cookies[i]);
            }
        }
        return cookieMap;
    }
    
    /**
     * 去除html代码
     * @param inputString
     * @return
     */
    public static String HtmltoText(String inputString) {
        String htmlStr = inputString; //含html标签的字符串
        String textStr ="";
        java.util.regex.Pattern p_script;
        java.util.regex.Matcher m_script;
        java.util.regex.Pattern p_style;
        java.util.regex.Matcher m_style;
        java.util.regex.Pattern p_html;
        java.util.regex.Matcher m_html;          
        java.util.regex.Pattern p_ba;
        java.util.regex.Matcher m_ba;
        
        try {
            String regEx_script = "<[\\s]*?script[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?script[\\s]*?>"; //定义script的正则表达式{或<script[^>]*?>[\\s\\S]*?<\\/script> }
            String regEx_style = "<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>"; //定义style的正则表达式{或<style[^>]*?>[\\s\\S]*?<\\/style> }
            String regEx_html = "<[^>]+>"; //定义HTML标签的正则表达式
            String patternStr = "\\s+";
            
            p_script = Pattern.compile(regEx_script,Pattern.CASE_INSENSITIVE);
            m_script = p_script.matcher(htmlStr);
            htmlStr = m_script.replaceAll(""); //过滤script标签

            p_style = Pattern.compile(regEx_style,Pattern.CASE_INSENSITIVE);
            m_style = p_style.matcher(htmlStr);
            htmlStr = m_style.replaceAll(""); //过滤style标签
         
            p_html = Pattern.compile(regEx_html,Pattern.CASE_INSENSITIVE);
            m_html = p_html.matcher(htmlStr);
            htmlStr = m_html.replaceAll(""); //过滤html标签
            
            p_ba = Pattern.compile(patternStr,Pattern.CASE_INSENSITIVE);
            m_ba = p_ba.matcher(htmlStr);
            htmlStr = m_ba.replaceAll(""); //过滤空格
         
         textStr = htmlStr;
         
        }catch(Exception e) {
                    System.err.println("Html2Text: " + e.getMessage());
        }          
        return textStr;//返回文本字符串
     }    
	
    /**
     * 获取项目的 basePath 路径,例如:http://uc.cun365.com/
     * @return
     */
    public static String getBasePath(HttpServletRequest request){
    	String path = request.getContextPath(); 
		String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
		return basePath;
    }
    
    public static String getBasePath(HttpServletRequest request,boolean flag){
		String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+"/";
		return basePath;
    }    
    
    /**
     * 清除缓存
     */
    @Deprecated
    public static void setBuffer(HttpServletResponse response){
        	response.setHeader("Pragma","No-cache"); 
        	response.setHeader("Cache-Control","no-cache"); 
        	response.setDateHeader("Expires", 0); 
        	try {
    			response.flushBuffer();
    		} catch (IOException e) {
    			System.out.println(new Date()+":清除 缓存时 出现异常");
    		}     		
    }
        
	public static String getUserName() {
		return userName;
	}

	public static void setUserName(String userName) {
		WebUtil.userName = userName;
	}

	public static String getDirectoryId() {
		return directoryId;
	}

	public static void setDirectoryId(String directoryId) {
		WebUtil.directoryId = directoryId;
	}
}



不好意思,忘记说 所读取的 资源文件了,
spring-jpa.properties
中的内容如下
###部署环境
jee=${jee}
ucenter.title=伟业中国用户中心
ucenter.url=http://www.xxx.com

mail.host=mail.xxx.com
mail.username=xxx
mail.password=xxx
[email protected]
mail.port=25

你可能感兴趣的:(apache,spring,正则表达式,velocity,jpa)