Android實(shí)現(xiàn)帶附件的郵件發(fā)送功能
本文實(shí)例講解了基于基于JMail實(shí)現(xiàn)Android郵件發(fā)送功能,分享給大家供大家參考,具體內(nèi)容如下
在android上發(fā)送郵件方式:
第一種:借助GMail APP客戶端,缺點(diǎn)是必須使用GMail帳號(hào),有一點(diǎn)是比較方便,不需要寫(xiě)很多代碼,但是不是很靈活。
第二種:基于JMail實(shí)現(xiàn),可以很靈活的自己設(shè)置各種屬性,不需要GMail帳號(hào)
在第二種方式的實(shí)現(xiàn)之前,看一下JMail對(duì)EMail結(jié)構(gòu)的劃分:
基于SMTP協(xié)議發(fā)送EMail,所以客戶端必須要知道SMTP的主機(jī)。
騰訊郵件的SMTP主機(jī)為:stmp.qq.com端口為465基于SSL協(xié)議。
最后我做了一個(gè)簡(jiǎn)單的封裝,把發(fā)送文本加圖像附件的功能做出了。
一個(gè)單獨(dú)的Class,只要調(diào)用一下即可完成:
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); //設(shè)置smtp的服務(wù)器地址 // props.put("mail.smtp.starttls.enable", "true"); // props.put("mail.smtp.port", port); // 設(shè)置端口 // props.put("mail.smtp.auth", "true"); //設(shè)置smtp服務(wù)器要身份驗(yàn)證。 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); // 返回授權(quán)Base64編碼 PopupAuthenticator auth = new PopupAuthenticator(userName, password); // 獲取會(huì)話對(duì)象 Session session = Session.getInstance(props, auth); // 設(shè)置為DEBUG模式 session.setDebug(true); // 郵件內(nèi)容對(duì)象組裝 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內(nèi)容 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); } } // 保存郵件內(nèi)容 message.setContent(multipart); // 獲取SMTP協(xié)議客戶端對(duì)象,連接到指定SMPT服務(wù)器 Transport transport = session.getTransport("smtp"); transport.connect(host, Integer.parseInt(port), userName, password); System.out.println("connet it success!!!!"); // 發(fā)送郵件到SMTP服務(wù)器 Thread.currentThread().setContextClassLoader( getClass().getClassLoader() ); Transport.send(message); System.out.println("send it success!!!!"); // 關(guān)閉連接 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; } }
用戶授權(quán)類:
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上發(fā)送郵件必須自己導(dǎo)入三個(gè)相關(guān)的JAVA文件
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)Android軟件編程有所幫助。
相關(guān)文章
android文件操作——讀取assets和raw文件下的內(nèi)容
本篇文章主要介紹了android文件操作——讀取assets和raw文件下的內(nèi)容,并附簡(jiǎn)單實(shí)例代碼,需要的朋友可以參考下。2016-10-10用MOB實(shí)例開(kāi)發(fā)實(shí)現(xiàn)短信驗(yàn)證功能
本篇文章通學(xué)習(xí)通過(guò)MOB平臺(tái)開(kāi)發(fā)APP實(shí)現(xiàn)簡(jiǎn)單的短信驗(yàn)證功能,對(duì)此有需求的朋友跟著好好學(xué)習(xí)下吧。2018-01-01android源碼探索之定制android關(guān)機(jī)界面的方法
這篇文章主要介紹了android源碼探索之定制android關(guān)機(jī)界面的方法,較為詳細(xì)的分析了Android關(guān)機(jī)界面的相關(guān)原理與代碼實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10Android使用DrawerLayout實(shí)現(xiàn)仿QQ雙向側(cè)滑菜單
這篇文章主要介紹了Android使用DrawerLayout實(shí)現(xiàn)仿QQ雙向側(cè)滑菜單的方法和詳細(xì)代碼,有需要的小伙伴可以認(rèn)真參考下。2016-01-01鴻蒙手機(jī)版JNI實(shí)戰(zhàn)案例解析(JNI開(kāi)發(fā)、SO庫(kù)生成、SO庫(kù)使用)
這篇文章主要介紹了鴻蒙手機(jī)版JNI實(shí)戰(zhàn)(JNI開(kāi)發(fā)、SO庫(kù)生成、SO庫(kù)使用)的相關(guān)資料,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04Android 調(diào)用系統(tǒng)應(yīng)用的方法總結(jié)
這篇文章主要介紹了Android 調(diào)用系統(tǒng)應(yīng)用的方法總結(jié)的相關(guān)資料,這里提供調(diào)用錄像,錄音,拍照等功能,需要的朋友可以參考下2017-08-08