使用JavaMail發送郵件需要用到mail.jar和activtion.jar兩個包。
該類實現了較完整的郵件發送功能,包括以HTML格式發送,添加附件和抄送人。下面是具體的代碼:
Mail.java:
package cn.cgw.util.mail; 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; public class Mail { private MimeMessage mimeMsg; //MIME郵件對象 private Session session; //郵件會話對象 private Properties props; //系統屬性 private boolean needAuth = false; //smtp是否需要認證 //smtp認證用戶名和密碼 private String username; private String password; private Multipart mp; //Multipart對象,郵件內容,標題,附件等內容均添加到其中後再生成MimeMessage對象 /** * Constructor * @param smtp 郵件發送服務器 */ public Mail(String smtp){ setSmtpHost(smtp); createMimeMessage(); } /** * 設置郵件發送服務器 * @param hostName String */ 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主機 } /** * 創建MIME郵件對象 * @return */ public boolean createMimeMessage() { try { System.out.println("准備獲取郵件會話對象!"); session = Session.getDefaultInstance(props,null); //獲得郵件會話對象 } catch(Exception e){ System.err.println("獲取郵件會話對象時發生錯誤!"+e); return false; } System.out.println("准備創建MIME郵件對象!"); try { mimeMsg = new MimeMessage(session); //創建MIME郵件對象 mp = new MimeMultipart(); return true; } catch(Exception e){ System.err.println("創建MIME郵件對象失敗!"+e); return false; } } /** * 設置SMTP是否需要驗證 * @param need */ 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"); } } /** * 設置用戶名和密碼 * @param name * @param pass */ public void setNamePass(String name,String pass) { username = name; password = pass; } /** * 設置郵件主題 * @param mailSubject * @return */ public boolean setSubject(String mailSubject) { System.out.println("設置郵件主題!"); try{ mimeMsg.setSubject(mailSubject); return true; } catch(Exception e) { System.err.println("設置郵件主題發生錯誤!"); return false; } } /** * 設置郵件正文 * @param mailBody String */ public boolean setBody(String mailBody) { try{ BodyPart bp = new MimeBodyPart(); bp.setContent(""+mailBody,"text/html;charset=GBK"); mp.addBodyPart(bp); return true; } catch(Exception e){ System.err.println("設置郵件正文時發生錯誤!"+e); return false; } } /** * 添加附件 * @param filename String */ public boolean addFileAffix(String filename) { System.out.println("增加郵件附件:"+filename); try{ BodyPart bp = new MimeBodyPart(); FileDataSource fileds = new FileDataSource(filename); bp.setDataHandler(new DataHandler(fileds)); bp.setFileName(fileds.getName()); mp.addBodyPart(bp); return true; } catch(Exception e){ System.err.println("增加郵件附件:"+filename+"發生錯誤!"+e); return false; } } /** * 設置發信人 * 查看本欄目public static void main(String[] args){ String smtp = "SMTP服務器"; String from = "發信人"; String to = "收信人"; String copyto = "抄送人"; String subject = "郵件主題"; String content = "郵件內容"; String username="用戶名"; String password="密碼"; String filename = "附件路徑,如:F:\\筆記<a>\\struts2</a>與mvc.txt"; Mail.sendAndCc(smtp, from, to, copyto, subject, content, username, password, filename); }