码迷,mamicode.com
首页 > 编程语言 > 详细

java 邮件发送多分附件

时间:2014-10-16 04:36:42      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:java 邮件发送多分附件

package com.sungrow.sendmail;


import java.io.File;

import java.util.ArrayList;

import java.util.Date;

import java.util.List;

import java.util.Properties;


import javax.activation.DataHandler;

import javax.activation.DataSource;

import javax.activation.FileDataSource;

import javax.mail.Authenticator;

import javax.mail.BodyPart;

import javax.mail.Message.RecipientType;

import javax.mail.PasswordAuthentication;

import javax.mail.Session;

import javax.mail.Transport;

import javax.mail.internet.InternetAddress;

import javax.mail.internet.MimeBodyPart;

import javax.mail.internet.MimeMessage;

import javax.mail.internet.MimeMultipart;

import javax.mail.internet.MimeUtility;


import org.slf4j.Logger;

import org.slf4j.LoggerFactory;


import com.sungrow.commons.ConfigDataService;

import com.sungrow.dao.entity.ReportMail;

import com.sungrow.exception.InsightDBException;

public class SendMail {

/**

* 发件箱 do_not_reply@fmclubs.com

*/

private static String FROM = null;

/**

* 发件箱密码fmc123

*/

private static  String PASSWORD = null;

private static Logger _log = LoggerFactory.getLogger("Task");

/***

* 发送邮件带附件(发给多人)

* @param url

*            附件路劲

* @param title

*            邮箱标题

* @param content

*            邮件内容

* @param email 接受者邮件

*/

public static boolean sendEmailAppendixList()throws InsightDBException {

Session session =getSession();

MimeMessage message = new MimeMessage(session);

String str="email1,email2,email3";//多分接受邮件者

try {

@SuppressWarnings("static-access")

InternetAddress[] iaToListcs = new InternetAddress().parse(str); 

MimeMultipart mm = new MimeMultipart();

message.setSubject("邮件标题");

message.setSentDate(new Date());

message.setFrom(new InternetAddress(FROM));

//message.setRecipient(RecipientType.TO, new InternetAddress(email));

message.setRecipients(RecipientType.CC,iaToListcs);//发送给多人

// 设置邮件内容

BodyPart body =null;

DataSource ds=null;

List<String> url=new ArrayList<String>();//多分附件的路劲

url.add("附件路劲");

for(int i=0;i<url.size();i++){

BodyPart contentPart = new MimeBodyPart();

contentPart.setText("邮件内容");

mm.addBodyPart(contentPart);

// 设置邮件附件

body = new MimeBodyPart();

ds= new FileDataSource(new File(url.get(i)));

DataHandler dh = new DataHandler(ds);

body.setDataHandler(dh);

body.setFileName(MimeUtility.encodeText(ds.getName(),"utf-8",null));

mm.addBodyPart(body);

}

message.setContent(mm);

// 发邮件

Transport.send(message);

} catch (Exception e) {

e.printStackTrace();

_log.error(e.getMessage());

_log.error("发送邮件失败======》sendEmailAppendixList");

return false;

}

return true;

}


/***

* 获得session

* @return

*/

public static Session getSession() {

ConfigDataService sd=ConfigDataService.getInstance();

//map=DaoFactory.getReportDaoInstance().selectEmail();

FROM=sd.getMail_addr();//发送者邮箱

PASSWORD=sd.getMail_psd();//发送者邮箱密码

Properties props = new Properties();

props.setProperty("mail.transport.protocol",sd.getMail_protocol());

props.put("mail.smtp.localhost", "localHostAdress");

props.setProperty("mail.smtp.host",sd.getMail_host());

props.setProperty("mail.smtp.port",sd.getMail_port());

props.setProperty("mail.smtp.auth", "true");

Session session = Session.getInstance(props, new Authenticator() {

@Override

protected PasswordAuthentication getPasswordAuthentication() {

return new PasswordAuthentication(FROM, PASSWORD);

}

});

return session;

}

}


本文出自 “逍遥求败” 博客,请务必保留此出处http://6355601.blog.51cto.com/6345601/1564586

java 邮件发送多分附件

标签:java 邮件发送多分附件

原文地址:http://6355601.blog.51cto.com/6345601/1564586

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!