发布于 2016-06-19 04:37:01 | 179 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

Android移动端操作系统

Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用“安卓”或“安致”。


这篇文章主要介绍了Android实现带附件的邮件发送功能的相关资料,android发送邮件有两种方式,本文重点介绍基于JMail实现邮件发送功能,感兴趣的小伙伴们可以参考一下

本文实例讲解了基于基于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软件编程有所帮助。



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务