JavaMail,提供給開發者處理電子郵件相關的編程介面。它是Sun發佈的用來處理email的API。它可以方便地執行一些常用的郵件傳輸。但它並沒有包含在JDK中,要使用JavaMail首先要下載javax.mail.jar下載地址:https://javaee.github.io/javamail ...
JavaMail,提供給開發者處理電子郵件相關的編程介面。它是Sun發佈的用來處理email的API。它可以方便地執行一些常用的郵件傳輸。但它並沒有包含在JDK中,要使用JavaMail首先要下載javax.mail.jar下載地址:https://javaee.github.io/javamail/
自定義驗證:
/** * 自定義驗證 * @author fly * @時間 2017-05-09 * */ public class MyAuthenticator extends Authenticator{ private String userName; private String password; public MyAuthenticator() { } public MyAuthenticator(String userName, String password) { this.userName = userName; this.password = password; } @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(userName, password); } }
封裝發郵件信息:
/** * 封裝發郵件信息 * @author fly * @時間 2017-05-09 */ 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 String getFromAddress() { return fromAddress; } public void setFromAddress(String fromAddress) { this.fromAddress = fromAddress; } 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 getPassword() { return password; } public void setPassword(String password) { this.password = password; } public boolean isValidate() { return validate; } public void setValidate(boolean validate) { this.validate = validate; } public String getSubject() { return subject; } public void setSubject(String subject) { this.subject = subject; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public String[] getAttachFileNames() { return attachFileNames; } public void setAttachFileNames(String[] attachFileNames) { this.attachFileNames = attachFileNames; } }
封裝發送郵件代碼
/** * 封裝發送郵件代碼 * @author fly * @時間 2017-05-09 * */ public class Email { /** * 以文本格式發送郵件 * @param mailInfo 待發送郵件信息 * @throws Exception */ public static void sendTextMail(MailSenderInfo mailInfo) throws Exception{ // 判斷是否需要身份認證 MyAuthenticator authenticator = null; Properties pro = mailInfo.getProperties(); if(mailInfo.isValidate()){ // 如果需要身份認證,則創建一個密碼驗證器 authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword()); } // 根據郵件會話屬性和密碼驗證器構造一個發送郵件的session Session sendMailSession = Session.getDefaultInstance(pro, authenticator); // 根據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); } /** * 以HTML格式發送郵件 * @param mailInfo 待發送郵件信息 * @throws Exception */ public static void sendHtmlMail(MailSenderInfo mailInfo) throws Exception{ // 判斷是否需要身份認證 MyAuthenticator authenticator = null; Properties pro = mailInfo.getProperties(); if(mailInfo.isValidate()){ // 如果需要身份認證,則創建一個密碼驗證器 authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword()); } // 根據郵件會話屬性和密碼驗證器構造一個發送郵件的session Session sendMailSession = Session.getDefaultInstance(pro, authenticator); // 根據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()); Multipart mainPart = new MimeMultipart(); // 創建一個包含HTML內容的MimeBodyPart BodyPart html = new MimeBodyPart(); html.setContent(mailInfo.getContent(), "text/html; charset=utf-8"); mainPart.addBodyPart(html); // 將MiniMultipart對象設置為郵件內容 mailMessage.setContent(mainPart); // 發送郵件 Transport.send(mailMessage); }
測試程式:
public class JavaMailTest { public static void main(String[] args) {
//設置郵件相關信息 MailSenderInfo mailInfo = new MailSenderInfo(); mailInfo.setMailServerHost("smtp.163.com"); mailInfo.setMailServerPort("25"); mailInfo.setValidate(true); mailInfo.setUserName("[email protected]"); mailInfo.setPassword(""); // 您的郵箱密碼,若你的郵箱開啟了客戶端授權密碼,則此處是您的客戶端授權密碼 mailInfo.setFromAddress("[email protected]"); mailInfo.setToAddress("[email protected]"); mailInfo.setSubject("這是一封測試郵件"); mailInfo.setContent("你好!這是一封測試郵件"); try { Email.sendTextMail(mailInfo); //Email.sendHtmlMail(mailInfo); } catch (Exception e) { e.printStackTrace(); } } }
發送成功截圖如下: