今天學習了一下JavaMail,javamail發送郵件確實是一個比較麻煩的問題。爲了之後使用方便,本身寫了段代碼,打成jar包,以方便之後使用。呵呵
如下三段代碼是個人所有代碼,朋友們若是想用,直接複製便可。html
第一個類:MailSenderInfo.java java
package com.util.mail; /** * 發送郵件須要使用的基本信息 *author by wangfun */ 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; } }
第二個類:SimpleMailSender.java
package com.util.mail; import java.util.Date; import java.util.Properties; import javax.mail.Address; import javax.mail.BodyPart; import javax.mail.Message; import javax.mail.MessagingException; 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 SimpleMailSender { /** * 以文本格式發送郵件 * @param mailInfo 待發送的郵件的信息 */ public boolean sendTextMail(MailSenderInfo mailInfo) { // 判斷是否須要身份認證 MyAuthenticator authenticator = null; Properties pro = mailInfo.getProperties(); if (mailInfo.isValidate()) { // 若是須要身份認證,則建立一個密碼驗證器 authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword()); } // 根據郵件會話屬性和密碼驗證器構造一個發送郵件的session Session sendMailSession = Session.getDefaultInstance(pro,authenticator); try { // 根據session建立一個郵件消息 Message mailMessage = new MimeMessage(sendMailSession); // 建立郵件發送者地址 Address from = new InternetAddress(mailInfo.getFromAddress()); // 設置郵件消息的發送者 mailMessage.setFrom(from); // 建立郵件的接收者地址,並設置到郵件消息中 Address to = new InternetAddress(mailInfo.getToAddress()); mailMessage.setRecipient(Message.RecipientType.TO,to); // 設置郵件消息的主題 mailMessage.setSubject(mailInfo.getSubject()); // 設置郵件消息發送的時間 mailMessage.setSentDate(new Date()); // 設置郵件消息的主要內容 String mailContent = mailInfo.getContent(); mailMessage.setText(mailContent); // 發送郵件 Transport.send(mailMessage); return true; } catch (MessagingException ex) { ex.printStackTrace(); } return false; } /** * 以HTML格式發送郵件 * @param mailInfo 待發送的郵件信息 */ public static boolean sendHtmlMail(MailSenderInfo mailInfo){ // 判斷是否須要身份認證 MyAuthenticator authenticator = null; Properties pro = mailInfo.getProperties(); //若是須要身份認證,則建立一個密碼驗證器 if (mailInfo.isValidate()) { authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword()); } // 根據郵件會話屬性和密碼驗證器構造一個發送郵件的session Session sendMailSession = Session.getDefaultInstance(pro,authenticator); try { // 根據session建立一個郵件消息 Message mailMessage = new MimeMessage(sendMailSession); // 建立郵件發送者地址 Address from = new InternetAddress(mailInfo.getFromAddress()); // 設置郵件消息的發送者 mailMessage.setFrom(from); // 建立郵件的接收者地址,並設置到郵件消息中 Address to = new InternetAddress(mailInfo.getToAddress()); // Message.RecipientType.TO屬性表示接收者的類型爲TO mailMessage.setRecipient(Message.RecipientType.TO,to); // 設置郵件消息的主題 mailMessage.setSubject(mailInfo.getSubject()); // 設置郵件消息發送的時間 mailMessage.setSentDate(new Date()); // MiniMultipart類是一個容器類,包含MimeBodyPart類型的對象 Multipart mainPart = new MimeMultipart(); // 建立一個包含HTML內容的MimeBodyPart BodyPart html = new MimeBodyPart(); // 設置HTML內容 html.setContent(mailInfo.getContent(), "text/html; charset=utf-8"); mainPart.addBodyPart(html); // 將MiniMultipart對象設置爲郵件內容 mailMessage.setContent(mainPart); // 發送郵件 Transport.send(mailMessage); return true; } catch (MessagingException ex) { ex.printStackTrace(); } return false; } }
第三個類:MyAuthenticator.java
package com.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); } }
下面給出使用上面三個類的代碼:
public static void main(String[] args){ //這個類主要是設置郵件 MailSenderInfo mailInfo = new MailSenderInfo(); mailInfo.setMailServerHost("smtp.163.com"); mailInfo.setMailServerPort("25"); mailInfo.setValidate(true); mailInfo.setUserName("han2000lei@163.com"); mailInfo.setPassword("**********");//您的郵箱密碼 mailInfo.setFromAddress("han2000lei@163.com"); mailInfo.setToAddress("han2000lei@163.com"); mailInfo.setSubject("設置郵箱標題 如http://www.guihua.org 中國桂花網"); mailInfo.setContent("設置郵箱內容 如http://www.guihua.org 中國桂花網 是中國最大桂花網站=="); //這個類主要來發送郵件 SimpleMailSender sms = new SimpleMailSender(); sms.sendTextMail(mailInfo);//發送文體格式 sms.sendHtmlMail(mailInfo);//發送html格式 }
最後,給出朋友們幾個注意的地方: