欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android實(shí)現(xiàn)帶附件的郵件發(fā)送功能

 更新時(shí)間:2016年01月14日 11:31:53   投稿:lijiao  
這篇文章主要介紹了Android實(shí)現(xiàn)帶附件的郵件發(fā)送功能的相關(guān)資料,android發(fā)送郵件有兩種方式,本文重點(diǎn)介紹基于JMail實(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)文章

最新評(píng)論