Javamail操作指南(二)

Bromon原创 请尊重版权
 二、 邮件的收取

 通常情况下我们都使用pop3协议来收邮件,IMAP嘛现在就不涉及了。收邮件的功能虽然我用了很多时间才基本搞清楚,不过讲起来就so easy了,一个程序就可以基本包括。

 邮件大致可以分三种:纯文本邮件、含有其他数据的文本邮件、含有附件的邮件。


 CODE  
 /*
 * Created on 2004-4-26
 */
 package org.bromon.mail;
 import javax.mail.*;
 import java.util.*;
 import java.io.*;

 /**
 * @author Bromon
 */
 public class Receiver
 {
 Folder inbox;
 Store store;

 //连接邮件服务器,获得所有邮件的列表
 public Message[] getMail(String host,String name,String password) throws Exception
 {
  Properties prop=new Properties();
  prop.put("mail.pop3.host",host);
  Session session=Session.getDefaultInstance(prop);
  store=session.getStore("pop3");
  store.connect(host,name,password);
  
  inbox=store.getDefaultFolder().getFolder("INBOX");
  inbox.open(Folder.READ_ONLY);
  
  Message[] msg=inbox.getMessages();
  
  FetchProfile profile=new FetchProfile();
  profile.add(FetchProfile.Item.ENVELOPE);
  inbox.fetch(msg,profile);
  
  return(msg);
 }

 //处理任何一种邮件都需要的方法
 private void handle(Message msg) throws Exception
 {
  System.out.println("邮件主题:"+msg.getSubject());
  System.out.println("邮件作者:"+msg.getFrom()[0].toString());
  System.out.println("发送日期:"+msg.getSentDate());
 }

 //处理文本邮件
 public void handleText(Message msg) throws Exception
 {
  this.handle(msg);
  System.out.println("邮件内容:"+msg.getContent());
 }

 //处理Multipart邮件,包括了保存附件的功能
 public void handleMultipart(Message msg) throws Exception
 {
  String disposition;
  BodyPart part;
  
  Multipart mp=(Multipart)msg.getContent();
  int mpCount=mp.getCount();//Miltipart的数量,用于除了多个part,比如多个附件
  for(int m=0;m<mpCount;m++)
  {
   this.handle(msg);
   
   part=mp.getBodyPart(m);
   disposition=part.getDisposition();
   if(disposition!=null && disposition.equals(Part.ATTACHMENT))//判断是否有附件
   {
    //this.saveAttach(part);//这个方法负责保存附件,注释掉是因为附件可能有病毒,请清理信箱之后再取掉注释
   }else{
    System.out.println(part.getContent());
   }
  }
 }

 private void saveAttach(BodyPart part) throws Exception
 {
  String temp=part.getFileName();//得到未经处理的附件名字
  String s=temp.substring(11,temp.indexOf("?=")-1);//去到header和footer
  
  //文件名一般都经过了base64编码,下面是解码
  String fileName=this.base64Decoder(s);
  System.out.println("有附件:"+fileName);
  
  InputStream in=part.getInputStream();
  FileOutputStream writer=new FileOutputStream(new File(fileName));
  byte[] content=new byte[255];
  int read=0;
  while((read=in.read(content))!=-1)
  {
   writer.write(content);
  }
  writer.close();
  in.close();
 }

 //base64解码
 private String base64Decoder(String s) throws Exception
 {
  sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();
  byte[] b=decoder.decodeBuffer(s);
  
  return(new String(b));
 }

 //关闭连接
 public void close() throws Exception
 {
  if(inbox!=null)
  {
   inbox.close(false);
  }
  
  if(store!=null)
  {
   store.close();
  }
 }

 public static void main(String args[])
 {
  String host="pop.163.com";
  String name="bromon";
  String password="My password";
  
  Receiver receiver=new Receiver();
  
  try
  {   
   Message[] msg=receiver.getMail(host,name,password);
   
   for(int i=0;i<msg.length;i++)
   {
    if(msg[i].isMimeType("text/*"))//判断邮件类型
    {
     receiver.handleText(msg[i]);
    }else{
     receiver.handleMultipart(msg[i]);
    }
    System.out.println("****************************");
   }
   receiver.close();
  }catch(Exception e)
  {
   System.out.println(e);
  }
 }
 } 


 没有习惯读java代码的兄弟可能会觉得麻烦了一点,其中有个小问题,下载的附件会再文件名后面加上一个”#”符号,不知道这是javamail的特别处理还是pop3的规范。通过程序更改文件名很简单,就不说了。对于email还有很多其他的操作,可以自己取查看一下javadoc,我就不影响大家探索的乐趣了。在Properties里配置代理服务器,可以让程序通过代理收发邮件,一般的HTTP、socks 4、socks 5都支持。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值