*
1,附件名亂碼解決
Multipart mm = new MimeMultipart();
BodyPart mdp = new MimeBodyPart();
mdp.setFileName(MimeUtility.encodeWord(attachname));
2,新方法發送附件
public void sendAttachByTemplate(File file, TBaseMailset from, String toEmail,String attachname) { String subject = "薪酬通知郵件"; try { getMailSession().setDebug(true); Message msg = new MimeMessage(getMailSession()); //發件人 if(from!=null&&!"".equals(from.getMail())){ msg.setFrom(new InternetAddress(from.getMail()));//郵箱地址 }else{ msg.setFrom(new InternetAddress("lantuo103@163.com"));//郵箱地址 } //收件人 msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmail)); //設置主題 msg.setSubject(subject); //發送時間 msg.setSentDate(new java.util.Date()); // 添加附件 Multipart mm = new MimeMultipart(); BodyPart mdp = new MimeBodyPart(); ///E:/JavaWorkspace/rs/WebRoot/WEB-INF/classes/xlsresult/ygxc.xls //File file = new File(this.getClass().getResource("/").getPath()+"xlsresult/ygxc.xls"); FileDataSource fds = new FileDataSource(file); DataHandler dh = new DataHandler(fds); //mdp.setFileName("salary.xls");//設置后綴為xls的名字后就可以發送excel附件了 //mdp.setFileName(attachname);//設置后綴為xls的名字后就可以發送excel附件了 mdp.setFileName(MimeUtility.encodeWord(attachname));//設置后綴為xls的名字后就可以發送excel附件了 //mdp.setFileName(URLEncoder.encode(attachname, "UTF-8")); mdp.setDataHandler(dh); mm.addBodyPart(mdp); msg.setContent(mm); Transport transport = mailSession.getTransport("smtp"); //transport.connect("smtp.163.com", "lantuo103@163.com", "lantuo"); if(from!=null){ //如果沒有設置發送郵箱 transport.connect(from.getServerHost(), from.getMail(), from.getMailPassword()); }else{ transport.connect("smtp.163.com", "lwww@163.com", "lwww"); } transport.sendMessage(msg, msg.getAllRecipients()); } catch (Exception ex) { ex.printStackTrace(); } }
package com.magnetic.project.util.mail; /** * 發送郵件需要使用的基本信息 */ import java.util.Properties; public class MailSenderInfo { // 發送郵件的服務器的IP和端口 private String mailServerHost; private String mailServerPort = "25"; // 郵件發送者的地址 private String fromAddress; // 郵件接收者的地址 private String toAddress; // 登陸郵件發送服務器的用戶名和密碼 private String userName; private String password; // 是否需要身份驗證 private boolean validate = false; // 郵件主題 private String subject; // 郵件的文本內容 private String content; // 郵件附件的文件名 private String[] attachFileNames; /** * 獲得郵件會話屬性 */ public Properties getProperties(){ Properties p = new Properties(); p.put("mail.smtp.host", this.mailServerHost); p.put("mail.smtp.port", this.mailServerPort); p.put("mail.smtp.auth", validate ? "true" : "false"); return p; } public String getMailServerHost() { return mailServerHost; } public void setMailServerHost(String mailServerHost) { this.mailServerHost = mailServerHost; } public String getMailServerPort() { return mailServerPort; } public void setMailServerPort(String mailServerPort) { this.mailServerPort = mailServerPort; } public boolean isValidate() { return validate; } public void setValidate(boolean validate) { this.validate = validate; } public String[] getAttachFileNames() { return attachFileNames; } public void setAttachFileNames(String[] fileNames) { this.attachFileNames = fileNames; } public String getFromAddress() { return fromAddress; } public void setFromAddress(String fromAddress) { this.fromAddress = fromAddress; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getToAddress() { return toAddress; } public void setToAddress(String toAddress) { this.toAddress = toAddress; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getSubject() { return subject; } public void setSubject(String subject) { this.subject = subject; } public String getContent() { return content; } public void setContent(String textContent) { this.content = textContent; } }
**
package com.magnetic.project.util.mail; import java.util.List; import java.util.Map; import java.util.Properties; import javax.activation.DataHandler; import javax.mail.BodyPart; import javax.mail.Message; import javax.mail.Multipart; 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 com.magnetic.project.po.TRsYgxc; public class MailService { Session mailSession = null; public Session getMailSession() { if (mailSession == null) { Properties props = new Properties(); props.put("mail.smtp.auth", "true"); mailSession = Session.getInstance(props, null); } return mailSession; } public void sendAttachBySys(Map<String, List<String>> map, String fromEmail, String toEmail) { String subject = "薪酬通知郵件"; try { getMailSession().setDebug(true); Message msg = new MimeMessage(getMailSession()); msg.setFrom(new InternetAddress(fromEmail)); msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmail)); msg.setSubject(subject); msg.setSentDate(new java.util.Date()); // 添加附件 Multipart mm = new MimeMultipart(); BodyPart mdp = new MimeBodyPart(); StringBuffer sb = new StringBuffer(); 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<String> 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 = new DataHandler(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 void sendAttachBySys(TRsYgxc ygxc, String fromEmail, String toEmail) { String subject = "薪酬通知郵件"; try { getMailSession().setDebug(true); Message msg = new MimeMessage(getMailSession()); msg.setFrom(new InternetAddress(fromEmail)); msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmail)); msg.setSubject(subject); msg.setSentDate(new java.util.Date()); // 添加附件 Multipart mm = new MimeMultipart(); BodyPart mdp = new MimeBodyPart(); StringBuffer sb = new StringBuffer(); 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 = new DataHandler(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(); } } }
**
package com.magnetic.project.util.mail; import javax.mail.*; public class MyAuthenticator extends Authenticator{ String userName=null; String password=null; public MyAuthenticator(){ } public MyAuthenticator(String username, String password) { this.userName = username; this.password = password; } protected PasswordAuthentication getPasswordAuthentication(){ return new PasswordAuthentication(userName, password); } }
**
*
