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();
}
}
}