java邮件中添加excel_javamail发送excel附件

packagecom.magnetic.project.util.mail;importjava.util.List;importjava.util.Map;importjava.util.Properties;importjavax.activation.DataHandler;importjavax.mail.BodyPart;importjavax.mail.Message;importjavax.mail.Multipart;importjavax.mail.Session;importjavax.mail.Transport;importjavax.mail.internet.InternetAddress;importjavax.mail.internet.MimeBodyPart;importjavax.mail.internet.MimeMessage;importjavax.mail.internet.MimeMultipart;importcom.magnetic.project.po.TRsYgxc;public classMailService {

Session mailSession= null;publicSession getMailSession() {if (mailSession == null) {

Properties props= newProperties();

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

mailSession= Session.getInstance(props, null);

}returnmailSession;

}public void sendAttachBySys(Map>map,

String fromEmail, String toEmail) {

String subject= "薪酬通知邮件";try{

getMailSession().setDebug(true);

Message msg= newMimeMessage(getMailSession());

msg.setFrom(newInternetAddress(fromEmail));

msg.setRecipients(Message.RecipientType.TO,

InternetAddress.parse(toEmail));

msg.setSubject(subject);

msg.setSentDate(newjava.util.Date());//添加附件

Multipart mm = newMimeMultipart();

BodyPart mdp= newMimeBodyPart();

StringBuffer sb= newStringBuffer();

String salaryTime=null;

sb.append("用户名\t工号\t薪酬月份\t基本工资\t绩效奖金\t其他补贴\t其他补贴备注\t其他奖励1\t其他奖励1备注\t其他奖励2"+

"\t其他奖励2备注\t过节费\t扣罚\t扣罚备注\t应发工资合计\t个人住房公积金\t个人基本养老保险"+

"\t个人医疗保险\t个人失业保险\t个人企业年金\t其他保险\t他代扣费用"+

"\t应税工资\t税金\t扣款合计\t互助前实发\t互助保险\t合计\t总部已发放\t代扣工会经费\t实发"+

"\t雇主住房公积金缴费\t雇主基本养老保险缴费\t雇主医疗保险缴费\t雇主生育保险缴费\t雇主工伤保险缴费"+

"\t雇主失业保险缴费\t雇主企业年金缴费\t雇主其他保险缴费\t误餐餐补\t交通费\t电话费"+

"\t差旅费\t其他1\t其他2\t其他合计\n");if (map != null && !map.isEmpty()) {for(String key : map.keySet()) {

List list =map.get(key);for (int i = 0; i < list.size(); i++) {

String value=list.get(i);if(i==2){

salaryTime=value;

}if (i == 0) {

sb.append(value);

}else{

sb.append("\t" +value);

}

}

sb.append("\n");

}

}

DataHandler dh= newDataHandler(sb.toString(),"text/plain;charset=gb2312");

mdp.setFileName("salary"+salaryTime+".xls");

mdp.setDataHandler(dh);

mm.addBodyPart(mdp);

msg.setContent(mm);

Transport transport= mailSession.getTransport("smtp");

transport.connect("smtp.163.com", "lantuo103@163.com", "lantuo");

transport.sendMessage(msg, msg.getAllRecipients());

}catch(Exception ex) {

ex.printStackTrace();

}

}public voidsendAttachBySys(TRsYgxc ygxc,

String fromEmail, String toEmail) {

String subject= "薪酬通知邮件";try{

getMailSession().setDebug(true);

Message msg= newMimeMessage(getMailSession());

msg.setFrom(newInternetAddress(fromEmail));

msg.setRecipients(Message.RecipientType.TO,

InternetAddress.parse(toEmail));

msg.setSubject(subject);

msg.setSentDate(newjava.util.Date());//添加附件

Multipart mm = newMimeMultipart();

BodyPart mdp= newMimeBodyPart();

StringBuffer sb= newStringBuffer();

sb.append("用户名\t工号\n");

System.out.println((ygxc.getGrqynj()==null)+"ttttttttttttt");

System.out.println((ygxc.getGrqynj())+"ttttttttttttt");

sb.append(ygxc.getUser().getUserName()+"\t"+ygxc.getUser().getUserLoginAcount()+"\t"+ygxc.getXcyf()==null?"":ygxc.getXcyf()+"\t"+DataHandler dh= newDataHandler(sb.toString(),"text/plain;charset=gb2312");

mdp.setFileName("salary"+ygxc.getXcyf()+".xls");

mdp.setDataHandler(dh);

mm.addBodyPart(mdp);

msg.setContent(mm);

Transport transport= mailSession.getTransport("smtp");

transport.connect("smtp.163.com", "lantuo103@163.com", "lantuo");

transport.sendMessage(msg, msg.getAllRecipients());

}catch(Exception ex) {

ex.printStackTrace();

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值