java 邮件发送 多附件_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 url=new ArrayList();//多分附件的路劲

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

for(int i=0;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;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值