這篇文章主要介紹了struts2集成javamail發郵件示例,需要的朋友可以參考下
一、代碼預覽 這兩天在做struts2上的郵件發送。以前的項目有用到spring,用spring提供的郵件支持類很方便可以完成這個功能,但是現在只用struts2的話,就碰到了一系列的問題。 請先將自己下載的 mail.jar,及activation.jar加入到classpath 代碼如下: package com.nerve.cloudoffice.common.util; import java.util.List; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.Address; 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 javax.mail.internet.MimeUtility; public class EMailSender { /** * 向用戶發送郵件 */ private static final long serialVersionUID = 1L; private MimeMessage mimeMsg; // MIME郵件對象 private Session session; // 郵件會話對象 private Properties props; // 系統屬性 private boolean needAuth = false; // smtp是否需要認證 private String username = ""; // smtp認證用戶名和密碼 private String password = ""; private Multipart mp; // Multipart對象,郵件內容,標題,附件等內容均添加到其中後再生成 private String log; public EMailSender() { } public EMailSender(String smtp) { setSmtpHost(smtp); createMimeMessage(); } public void setSmtpHost(String hostName) { System.out.println("設置系統屬性:mail.smtp.host = " + hostName); if (props == null) props = System.getProperties(); // 獲得系統屬性對象 props.put("mail.smtp.host", hostName); // 設置SMTP主機 props.put("mail.smtp.localhost", "localHostAdress"); } public boolean createMimeMessage() { try { System.out.println("准備獲取郵件會話對象!"); session = Session.getDefaultInstance(props, null); // 獲得郵件會話對象 } catch (Exception e) { log = "獲取郵件會話對象時發生錯誤!" + e.toString(); System.err.println(log); return false; } try { mimeMsg = new MimeMessage(session); // 創建MIME郵件對象 mp = new MimeMultipart(); // mp 一個multipart對象 // Multipart is a container that holds multiple body parts. return true; } catch (Exception e) { log = "創建MIME郵件對象失敗!" + e; System.err.println(log); return false; } } public void setNeedAuth(boolean need) { System.out.println("設置smtp身份認證:mail.smtp.auth = " + need); if (props == null) props = System.getProperties(); if (need) { props.put("mail.smtp.auth", "true"); } else { props.put("mail.smtp.auth", "false"); } } public void setNamePass(String name, String pass) { System.out.println("程序得到用戶名與密碼"); username = name; password = pass; } public boolean setSubject(String mailSubject) { System.out.println("設置郵件主題!"); try { mimeMsg.setSubject(MimeUtility.encodeText(mailSubject,"utf-8","B")); return true; } catch (Exception e) { log = "設置郵件主題發生錯誤!"+e; System.err.println(log); return false; } } public boolean setBody(String mailBody) { try { System.out.println("設置郵件體格式"); BodyPart bp = new MimeBodyPart(); // 轉換成中文格式 bp.setContent( "<meta http-equiv=Content-Type content=text/html; charset=utf-8>" + mailBody, "text/html;charset=utf-8"); mp.addBodyPart(bp); return true; } catch (Exception e) { log = "設置郵件正文時發生錯誤!" + e; System.err.println(log); return false; } } public boolean setFiles(List<String> files){ try{ for(String s:files){ MimeBodyPart mbp=new MimeBodyPart(); FileDataSource fds=new FileDataSource(s); //得到數據源 mbp.setDataHandler(new DataHandler(fds)); //得到附件本身並至入BodyPart mbp.setFileName(fds.getName()); //得到文件名同樣至入BodyPart mp.addBodyPart(mbp); } return true; }catch(Exception e){ log = "增加附件時出錯:"+e; e.printStackTrace(); return false; } } public boolean addFile(String path, String name){ try{ MimeBodyPart mbp=new MimeBodyPart(); FileDataSource fds=new FileDataSource(path); //得到數據源 mbp.setDataHandler(new DataHandler(fds)); //得到附件