- 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 {
-
-
-
-
-
-
-
- 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();
- }
-
-
-
-
-
-
-
-
-
-
-
-
- public static void sendMultiPartEmail(String subject,String contents,String [] userEmailAddress,String fromEmailAddress,String []multiPaths) throws Exception
- {
-
-
-
-
-
- List list=new ArrayList();
-
- for(int j=0;j<multiPaths.length;j++)
- {
- EmailAttachment attachment = new EmailAttachment();
- if(multiPaths[j].indexOf("http")==-1)
- {
- 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);
- for(int a=0;a<list.size();a++)
- {
- email.attach((EmailAttachment)list.get(a));
- }
-
- email.send();
- }
-
-
-
-
-
-
-
-
-
-
- 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();
- }
-
-
-
-
-
- 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());
- }
- }
- }
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());
}
}
}