浏览 5110 次
锁定老帖子 主题:commons-email的使用封装
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2006-11-16
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()); } } } 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2006-11-16
注意要下载JAVAMAIL包
|
|
返回顶楼 | |