今天學習了一下JavaMail,javamail發送郵件確實是一個比較麻煩的問題不用第三方郵件程序。爲了之後使用方便,本身寫了段代碼.html
Javamail-Android配置步驟:java
下載Android版本JavaMail包,additional.jar、mail.jar和activation.jar,下載地址JavaMail-Androidandroid
在項目與src同一目錄級別下,新建文件夾lib,將下載的3個jar包放入該文件夾服務器
右鍵->Properties->Java Build Path->Libraries,選擇Add External JARs,找到項目下lib目錄的3個jar包網絡
個人代碼有三個類:
第一個類:MailSenderInfo.javasession
package com.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; } }
第二個類:MultiMailsender.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 MultiMailsender { /** * 以文本格式發送郵件 * @param mailInfo 待發送的郵件的信息 */ public boolean sendTextMail(MultiMailSenderInfo 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[] tos = null; String[] receivers = mailInfo.getReceivers(); if (receivers != null){ // 爲每一個郵件接收者建立一個地址 tos = new InternetAddress[receivers.length + 1]; tos[0] = new InternetAddress(mailInfo.getToAddress()); for (int i=0; i<receivers.length; i++){ tos[i+1] = new InternetAddress(receivers[i]); } } else { tos = new InternetAddress[1]; tos[0] = new InternetAddress(mailInfo.getToAddress()); } // Message.RecipientType.TO屬性表示接收者的類型爲TO mailMessage.setRecipients(Message.RecipientType.TO,tos); // 設置郵件消息的主題 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 帶發送郵件的信息 * @return */ public static boolean sendMailtoMultiReceiver(MultiMailSenderInfo mailInfo){ MyAuthenticator authenticator = null; if (mailInfo.isValidate()) { authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword()); } Session sendMailSession = Session.getInstance(mailInfo .getProperties(), authenticator); try { Message mailMessage = new MimeMessage(sendMailSession); // 建立郵件發送者地址 Address from = new InternetAddress(mailInfo.getFromAddress()); mailMessage.setFrom(from); // 建立郵件的接收者地址,並設置到郵件消息中 Address[] tos = null; String[] receivers = mailInfo.getReceivers(); if (receivers != null){ // 爲每一個郵件接收者建立一個地址 tos = new InternetAddress[receivers.length + 1]; tos[0] = new InternetAddress(mailInfo.getToAddress()); for (int i=0; i<receivers.length; i++){ tos[i+1] = new InternetAddress(receivers[i]); } } else { tos = new InternetAddress[1]; tos[0] = new InternetAddress(mailInfo.getToAddress()); } // 將全部接收者地址都添加到郵件接收者屬性中 mailMessage.setRecipients(Message.RecipientType.TO, tos); mailMessage.setSubject(mailInfo.getSubject()); mailMessage.setSentDate(new Date()); // 設置郵件內容 Multipart mainPart = new MimeMultipart(); BodyPart html = new MimeBodyPart(); html.setContent(mailInfo.getContent(), "text/html; charset=GBK"); mainPart.addBodyPart(html); mailMessage.setContent(mainPart); // 發送郵件 Transport.send(mailMessage); return true; } catch (MessagingException ex) { ex.printStackTrace(); } return false; } /** * 發送帶抄送的郵件 * @param mailInfo 待發送郵件的消息 * @return */ public static boolean sendMailtoMultiCC(MultiMailSenderInfo mailInfo){ MyAuthenticator authenticator = null; if (mailInfo.isValidate()) { authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword()); } Session sendMailSession = Session.getInstance(mailInfo .getProperties(), authenticator); try { 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); // 獲取抄送者信息 String[] ccs = mailInfo.getCcs(); if (ccs != null){ // 爲每一個郵件接收者建立一個地址 Address[] ccAdresses = new InternetAddress[ccs.length]; for (int i=0; i<ccs.length; i++){ ccAdresses[i] = new InternetAddress(ccs[i]); } // 將抄送者信息設置到郵件信息中,注意類型爲Message.RecipientType.CC mailMessage.setRecipients(Message.RecipientType.CC, ccAdresses); } mailMessage.setSubject(mailInfo.getSubject()); mailMessage.setSentDate(new Date()); // 設置郵件內容 Multipart mainPart = new MimeMultipart(); BodyPart html = new MimeBodyPart(); html.setContent(mailInfo.getContent(), "text/html; charset=GBK"); mainPart.addBodyPart(html); mailMessage.setContent(mainPart); // 發送郵件 Transport.send(mailMessage); return true; } catch (MessagingException ex) { ex.printStackTrace(); } return false; } /** * 發送多接收者類型郵件的基本信息 */ public static class MultiMailSenderInfo extends MailSenderInfo{ // 郵件的接收者,能夠有多個 private String[] receivers; // 郵件的抄送者,能夠有多個 private String[] ccs; public String[] getCcs() { return ccs; } public void setCcs(String[] ccs) { this.ccs = ccs; } public String[] getReceivers() { return receivers; } public void setReceivers(String[] receivers) { this.receivers = receivers; } } }
第三個類: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){ //這個類主要是設置郵件 MultiMailSenderInfo mailInfo = new MultiMailSenderInfo(); mailInfo.setMailServerHost("smtp.163.com"); mailInfo.setMailServerPort("25"); mailInfo.setValidate(true); mailInfo.setUserName("xxx@163.com"); mailInfo.setPassword("**********");//您的郵箱密碼 mailInfo.setFromAddress("xxx@163.com"); mailInfo.setToAddress("xxx@163.com"); mailInfo.setSubject("設置郵箱標題"); mailInfo.setContent("設置郵箱內容"); String[] receivers = new String[]{"***@163.com", "***@tom.com"}; String[] ccs = receivers; mailInfo.setReceivers(receivers); mailInfo.setCcs(ccs); //這個類主要來發送郵件 MultiMailsender sms = new MultiMailsender(); sms.sendTextMail(mailInfo);//發送文體格式 MultiMailsender.sendHtmlMail(mailInfo);//發送html格式 MultiMailsender.sendMailtoMultiCC(mailInfo);//發送抄送
最後,給出朋友們幾個注意的地方:
一、使用此代碼你能夠完成你的javamail的郵件發送功能、發多個郵箱。三個類缺一不可。
二、這三個類我打包是用的com.util.mail包,若是不喜歡,你能夠本身改,但三個類文件必須在同一個包中
三、不要使用你剛剛註冊過的郵箱在程序中發郵件,若是你的163郵箱是剛註冊不久,那你就不要使用「smtp.163.com」。由於你發不出去。剛註冊的郵箱是不會給你這種權限的,也就是你不能經過驗證。要使用你常常用的郵箱,並且時間比較長的。
四、另外一個問題就是mailInfo.setMailServerHost("smtp.163.com");與mailInfo.setFromAddress("xxx@163.com");這兩句話。即若是你使用163smtp服務器,那麼發送郵件地址就必須用163的郵箱,若是不的話,是不會發送成功的。
五、關於javamail驗證錯誤的問題,網上的解釋有不少,但我看見的只有一個。就是個人第三個類。你只要複製全了代碼,我想是不會有問題的。學習
六、 而後在Android項目中添加網絡訪問權限ui
<uses-permission android:name="android.permission.INTERNET"></uses-permission>this