commons-email的使用封装

[code]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());
}
}
}
[/code]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值