本文執行個體講解了基於基於JMail實現Android郵件發送功能,分享給大家供大家參考,具體內容如下
在android上發送郵件方式:
第一種:藉助GMail APP用戶端,缺點是必須使用GMail帳號,有一點是比較方便,不需要寫很多代碼,但是不是很靈活。
第二種:基於JMail實現,可以很靈活的自己設定各種屬性,不需要GMail帳號
在第二種方式的實現之前,看一下JMail對EMail結構的劃分:
基於SMTP協議發送EMail,所以用戶端必須要知道SMTP的主機。
騰訊郵件的SMTP主機為:stmp.qq.com連接埠為465基於SSL協議。
最後我做了一個簡單的封裝,把發送文本加映像附件的功能做出了。
一個單獨的Class,只要調用一下即可完成:
package com.gloomyfish.jmail.demo; import java.util.Date; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.DataSource; import javax.activation.FileDataSource; import javax.mail.Address; 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 EMailSender { private String host; private String port; private String userName; private String password; private String[] images; public String[] getImagePath() { return images; } public void setImagePath(String[] imagePath) { this.images = imagePath; } public EMailSender(String host, String port, String userName, String password) { this.host = host; this.port = port; this.userName = userName; this.password = password; } public void sendEmail(String subject, String recepits, String sender, String content) { Properties props = new Properties(); props.put("mail.smtp.host", host); //設定smtp的伺服器位址 // props.put("mail.smtp.starttls.enable", "true"); // props.put("mail.smtp.port", port); // 設定連接埠 // props.put("mail.smtp.auth", "true"); //設定smtp伺服器要身分識別驗證。 props.put("mail.smtp.socketFactory.port", port); props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.port", port); // 返回授權Base64編碼 PopupAuthenticator auth = new PopupAuthenticator(userName, password); // 擷取會話對象 Session session = Session.getInstance(props, auth); // 設定為DEBUG模式 session.setDebug(true); // 郵件內容對象組裝 MimeMessage message = new MimeMessage(session); try { Address addressFrom = new InternetAddress(sender, "Jia Zhi Gang"); Address addressTo = new InternetAddress(recepits, "My QQ E-Mail"); message.setSubject(subject); message.setSentDate(new Date()); message.setFrom(addressFrom); message.addRecipient(Message.RecipientType.TO,addressTo); // 郵件文本/HTML內容 Multipart multipart = new MimeMultipart(); MimeBodyPart messageBodyPart = new MimeBodyPart(); messageBodyPart.setContent(content, "text/html"); multipart.addBodyPart(messageBodyPart); // 添加郵件附件 if (images != null && images.length > 0) { for (String filePath : images) { MimeBodyPart attachPart = new MimeBodyPart(); DataSource source = new FileDataSource(filePath); attachPart.setDataHandler(new DataHandler(source)); attachPart.setFileName(filePath); multipart.addBodyPart(attachPart); } } // 儲存郵件內容 message.setContent(multipart); // 擷取SMTP協議用戶端對象,串連到指定SMPT伺服器 Transport transport = session.getTransport("smtp"); transport.connect(host, Integer.parseInt(port), userName, password); System.out.println("connet it success!!!!"); // 發送郵件到SMTP伺服器 Thread.currentThread().setContextClassLoader( getClass().getClassLoader() ); Transport.send(message); System.out.println("send it success!!!!"); // 關閉串連 transport.close(); } catch(Exception e) { e.printStackTrace(); } } public String getHost() { return host; } public void setHost(String host) { this.host = host; } public String getPort() { return port; } public void setPort(String port) { this.port = port; } 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; } }
使用者授權類:
package com.gloomyfish.jmail.demo; import javax.mail.Authenticator; import javax.mail.PasswordAuthentication; class PopupAuthenticator extends Authenticator { private String userName; private String password; public PopupAuthenticator(String userName, String password) { this.userName = userName; this.password = password; } public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(userName, password); } }
特別注意:
在android上發送郵件必須自己匯入三個相關的JAVA檔案
以上就是本文的全部內容,希望對大家的學習Android軟體編程有所協助。