日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

android 信息添加附件功能,Android实现带附件的邮件发送功能

發布時間:2024/9/27 Android 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android 信息添加附件功能,Android实现带附件的邮件发送功能 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本文實例講解了基于基于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軟件編程有所幫助。

總結

以上是生活随笔為你收集整理的android 信息添加附件功能,Android实现带附件的邮件发送功能的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。