欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android实现带附件的邮件发送功能

程序员文章站 2024-02-16 08:13:04
本文实例讲解了基于基于jmail实现android邮件发送功能,分享给大家供大家参考,具体内容如下 在android上发送邮件方式: 第一种:借助gmail app客户...

本文实例讲解了基于基于jmail实现android邮件发送功能,分享给大家供大家参考,具体内容如下

在android上发送邮件方式:

第一种:借助gmail app客户端,缺点是必须使用gmail帐号,有一点是比较方便,不需要写很多代码,但是不是很灵活。

第二种:基于jmail实现,可以很灵活的自己设置各种属性,不需要gmail帐号

在第二种方式的实现之前,看一下jmail对email结构的划分:

Android实现带附件的邮件发送功能

基于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软件编程有所帮助。