commons-email的使用封装

Java代码 复制代码
  1. package org.apache.commons.mail;   
  2. import java.net.URL;   
  3. import java.util.ArrayList;   
  4. import java.util.List;   
  5.   
  6. import com.hisunsray.travel.config.Config;   
  7.   
  8. public class MailUtils {   
  9.     /**  
  10.      * 简单的发邮件方式    邮件内容只有标题和邮件内容  支持多个用户批量发送  
  11.      * @param subject   邮件标题  
  12.      * @param contents  邮件内容  
  13.      * @param userEmailAddress   收入人的邮件地址  为数组形式  
  14.      * @throws Exception   
  15.      */  
  16.     public static void sendSimpleEmail(String subject,String contents,String [] userEmailAddress,String fromEmailAddress) throws Exception   
  17.     {   
  18.         Config config = new Config();   
  19.         config.loadProperty();   
  20.         SimpleEmail email = new SimpleEmail();   
  21.         email.setHostName(Config.getProperty("mailHostName"));   
  22.         email.setAuthentication(Config.getProperty("mailHostUserName"),Config.getProperty("mailHostPassword"));   
  23.         //发送给多个人   
  24.         for(int i=0;i<userEmailAddress.length;i++)   
  25.         {   
  26.             email.addTo(userEmailAddress[i],userEmailAddress[i]);   
  27.         }   
  28.         email.setFrom(fromEmailAddress,fromEmailAddress);   
  29.         email.setSubject(subject);   
  30.         email.setContent(contents, "text/plain;charset=GBK");    
  31.         email.send();   
  32.     }   
  33.        
  34.     /**  
  35.      * 发送带附件的邮件方式  邮件内容有标题和邮件内容和附件,附件可以是本地机器上的文本,也可以是web上的一个URL 文件,   
  36.      * 当为web上的一个URL文件时,此方法可以将WEB中的URL文件先下载到本地,再发送给收入用户  
  37.      * @param subject   邮件标题  
  38.      * @param contents  邮件内容  
  39.      * @param userEmailAddress   收入人的邮件地址  为数组形式  
  40.      * @param multiPaths         附件地址         为数组形式  
  41.      * @throws Exception   
  42.      * @throws Exception   
  43.      */  
  44.        
  45.     public static void sendMultiPartEmail(String subject,String contents,String [] userEmailAddress,String fromEmailAddress,String []multiPaths) throws Exception   
  46.     {   
  47. //      MimeUtility.encodeText(filename);   测试中文乱码用的   
  48. //      EmailAttachment attachment = new EmailAttachment();   
  49. //      attachment.setPath("D:/hibernate培训讲座.ppt");   
  50. //      attachment.setDisposition(EmailAttachment.ATTACHMENT);   
  51. //      attachment.setDescription("Picture of John");   
  52.         List list=new ArrayList();   
  53. //      EmailAttachment [] attachmentArray = new EmailAttachment[multiPaths.length];   
  54.         for(int j=0;j<multiPaths.length;j++)   
  55.         {   
  56.             EmailAttachment attachment = new EmailAttachment();   
  57.             if(multiPaths[j].indexOf("http")==-1)   //判断当前这个文件路径是否在本地  如果是:setPath  否则  setURL;   
  58.             {   
  59.             attachment.setPath(multiPaths[j]);   
  60.             }   
  61.             else  
  62.             {   
  63.                 attachment.setURL(new URL(multiPaths[j]));   
  64.             }   
  65.             attachment.setDisposition(EmailAttachment.ATTACHMENT);   
  66.             attachment.setDescription("Picture of John");   
  67.             list.add(attachment);   
  68.         }   
  69.         //发送邮件信息   
  70.         Config config = new Config();   
  71.         config.loadProperty();   
  72.         MultiPartEmail email = new MultiPartEmail();   
  73.         email.setHostName(Config.getProperty("mailHostName"));   
  74.         email.setAuthentication(Config.getProperty("mailHostUserName"),Config.getProperty("mailHostPassword"));   
  75.         //发送给多个人   
  76.         for(int i=0;i<userEmailAddress.length;i++)   
  77.         {   
  78.             email.addTo(userEmailAddress[i],userEmailAddress[i]);   
  79.         }   
  80.         email.setFrom(fromEmailAddress,fromEmailAddress);   
  81.         email.setSubject("The picture");   
  82.         email.setMsg(contents);   //注意这个不要使用setContent这个方法  setMsg不会出现乱码   
  83.         for(int a=0;a<list.size();a++)   //添加多个附件   
  84.         {   
  85.              email.attach((EmailAttachment)list.get(a));   
  86.         }   
  87. //      email.attach(attachment);   
  88.         email.send();   
  89.     }   
  90.        
  91.     /**  
  92.      * 发送Html格式的邮件  
  93.      * @param subject   邮件标题  
  94.      * @param contents  邮件内容  
  95.      * @param userEmailAddress  接收用户的邮箱地址  
  96.      * @param fromEmailAddress  发送人的邮箱地址  
  97.      *          
  98.      * @throws Exception  
  99.      */  
  100.     public static void sendHtmlEmail(String subject,String contents,String [] userEmailAddress,String fromEmailAddress) throws Exception   
  101.     {   
  102.         Config config = new Config();   
  103.         config.loadProperty();   
  104.         HtmlEmail email = new HtmlEmail();   
  105.         email.setHostName(Config.getProperty("mailHostName"));   
  106.         email.setAuthentication(Config.getProperty("mailHostUserName"),Config.getProperty("mailHostPassword"));   
  107.         //发送给多个人   
  108.         for(int i=0;i<userEmailAddress.length;i++)   
  109.         {   
  110.             email.addTo(userEmailAddress[i],userEmailAddress[i]);   
  111.         }   
  112.         email.setFrom(fromEmailAddress,fromEmailAddress);   
  113.         email.setSubject(subject);   
  114.         email.setHtmlMsg(contents);   
  115.         email.setTextMsg(contents);   
  116.         email.send();   
  117.     }   
  118.     /**  
  119.      * 统一的发送邮件的方法   调用时一定要实例化EmailBean对象     
  120.      * @throws Exception   
  121.      *  
  122.      */  
  123.     public static void sendEmail(EmailBean bean) throws Exception   
  124.     {   
  125.         if(bean.isHaveMultPaths())   
  126.         {   
  127.             sendMultiPartEmail(bean.getSubject(),bean.getContents(),bean.getUserEmailAddress(),bean.getFromEmailAddress(),bean.getMultiPaths());   
  128.         }   
  129.         else  
  130.         {   
  131.             sendSimpleEmail(bean.getSubject(),bean.getContents(),bean.getUserEmailAddress(),bean.getFromEmailAddress());   
  132.         }   
  133.     }   
  134. }  
package org.apache.commons.mail;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import com.hisunsray.travel.config.Config;

public class MailUtils {
	/**
	 * 简单的发邮件方式    邮件内容只有标题和邮件内容  支持多个用户批量发送
	 * @param subject   邮件标题
	 * @param contents  邮件内容
	 * @param userEmailAddress   收入人的邮件地址  为数组形式
	 * @throws Exception 
	 */
	public static void sendSimpleEmail(String subject,String contents,String [] userEmailAddress,String fromEmailAddress) throws Exception
	{
		Config config = new Config();
	    config.loadProperty();
		SimpleEmail email = new SimpleEmail();
		email.setHostName(Config.getProperty("mailHostName"));
		email.setAuthentication(Config.getProperty("mailHostUserName"),Config.getProperty("mailHostPassword"));
		//发送给多个人
		for(int i=0;i<userEmailAddress.length;i++)
		{
			email.addTo(userEmailAddress[i],userEmailAddress[i]);
		}
		email.setFrom(fromEmailAddress,fromEmailAddress);
		email.setSubject(subject);
		email.setContent(contents, "text/plain;charset=GBK"); 
		email.send();
	}
	
	/**
	 * 发送带附件的邮件方式  邮件内容有标题和邮件内容和附件,附件可以是本地机器上的文本,也可以是web上的一个URL 文件, 
	 * 当为web上的一个URL文件时,此方法可以将WEB中的URL文件先下载到本地,再发送给收入用户
	 * @param subject   邮件标题
	 * @param contents  邮件内容
	 * @param userEmailAddress   收入人的邮件地址  为数组形式
	 * @param multiPaths         附件地址         为数组形式
	 * @throws Exception 
	 * @throws Exception 
	 */
	
	public static void sendMultiPartEmail(String subject,String contents,String [] userEmailAddress,String fromEmailAddress,String []multiPaths) throws Exception
	{
//		MimeUtility.encodeText(filename);   测试中文乱码用的
//		EmailAttachment attachment = new EmailAttachment();
//		attachment.setPath("D:/hibernate培训讲座.ppt");
//		attachment.setDisposition(EmailAttachment.ATTACHMENT);
//		attachment.setDescription("Picture of John");
		List list=new ArrayList();
//		EmailAttachment [] attachmentArray = new EmailAttachment[multiPaths.length];
		for(int j=0;j<multiPaths.length;j++)
		{
			EmailAttachment attachment = new EmailAttachment();
			if(multiPaths[j].indexOf("http")==-1)   //判断当前这个文件路径是否在本地  如果是:setPath  否则  setURL;
			{
			attachment.setPath(multiPaths[j]);
			}
			else
			{
				attachment.setURL(new URL(multiPaths[j]));
			}
			attachment.setDisposition(EmailAttachment.ATTACHMENT);
			attachment.setDescription("Picture of John");
			list.add(attachment);
		}
		//发送邮件信息
		Config config = new Config();
	    config.loadProperty();
	    MultiPartEmail email = new MultiPartEmail();
		email.setHostName(Config.getProperty("mailHostName"));
		email.setAuthentication(Config.getProperty("mailHostUserName"),Config.getProperty("mailHostPassword"));
		//发送给多个人
		for(int i=0;i<userEmailAddress.length;i++)
		{
			email.addTo(userEmailAddress[i],userEmailAddress[i]);
		}
		email.setFrom(fromEmailAddress,fromEmailAddress);
		email.setSubject("The picture");
		email.setMsg(contents);   //注意这个不要使用setContent这个方法  setMsg不会出现乱码
		for(int a=0;a<list.size();a++)   //添加多个附件
		{
			 email.attach((EmailAttachment)list.get(a));
		}
//	    email.attach(attachment);
		email.send();
	}
	
	/**
	 * 发送Html格式的邮件
	 * @param subject   邮件标题
	 * @param contents  邮件内容
	 * @param userEmailAddress  接收用户的邮箱地址
	 * @param fromEmailAddress  发送人的邮箱地址
	 *        
	 * @throws Exception
	 */
	public static void sendHtmlEmail(String subject,String contents,String [] userEmailAddress,String fromEmailAddress) throws Exception
	{
		Config config = new Config();
	    config.loadProperty();
	    HtmlEmail email = new HtmlEmail();
		email.setHostName(Config.getProperty("mailHostName"));
		email.setAuthentication(Config.getProperty("mailHostUserName"),Config.getProperty("mailHostPassword"));
		//发送给多个人
		for(int i=0;i<userEmailAddress.length;i++)
		{
			email.addTo(userEmailAddress[i],userEmailAddress[i]);
		}
		email.setFrom(fromEmailAddress,fromEmailAddress);
		email.setSubject(subject);
		email.setHtmlMsg(contents);
		email.setTextMsg(contents);
		email.send();
	}
	/**
	 * 统一的发送邮件的方法   调用时一定要实例化EmailBean对象   
	 * @throws Exception 
	 *
	 */
	public static void sendEmail(EmailBean bean) throws Exception
	{
		if(bean.isHaveMultPaths())
		{
			sendMultiPartEmail(bean.getSubject(),bean.getContents(),bean.getUserEmailAddress(),bean.getFromEmailAddress(),bean.getMultiPaths());
		}
		else
		{
			sendSimpleEmail(bean.getSubject(),bean.getContents(),bean.getUserEmailAddress(),bean.getFromEmailAddress());
		}
	}
}

你可能感兴趣的:(apache,Hibernate,Web,bean,J#)