<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆 - 24  文章 - 6  trackbacks - 0
    <2005年3月>
    272812345
    6789101112
    13141516171819
    20212223242526
    272829303112
    3456789

    常用鏈接

    隨筆分類(23)

    積分與排名

    • 積分 - 13765
    • 排名 - 2082

    最新評論

     二、 郵件的收取

    Bromon原創 請尊重版權

       通常情況下我們都使用pop3協議來收郵件,IMAP嘛現在就不涉及了。收郵件的功能雖然我用了很多時間才基本搞清楚,不過講起來就so easy了,一個程序就可以基本包括。

       郵件大致可以分三種:純文本郵件、含有其他數據的文本郵件、含有附件的郵件。

    /*
     * 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都支持。

    posted on 2005-03-28 13:26 Sometimes Java 閱讀(437) 評論(0)  編輯  收藏 所屬分類: Tech Flow
    主站蜘蛛池模板: 女人被男人桶得好爽免费视频| 亚洲国产精品人人做人人爱| 亚洲狠狠色丁香婷婷综合| 在线观看无码AV网站永久免费| 亚洲精品午夜国产va久久| 亚洲国产人成精品| 最近的中文字幕大全免费8| 亚洲AV无码AV吞精久久| 精品国产亚洲一区二区三区| 日本三级2019在线观看免费| 五月婷婷免费视频| www.亚洲成在线| 亚洲精品美女久久777777| 免费高清小黄站在线观看| 免费国产成人午夜在线观看| 337P日本欧洲亚洲大胆艺术图| 亚洲网站视频在线观看| 亚洲国产成人精品久久久国产成人一区二区三区综 | 一级做a爱过程免费视| 亚洲黄色片在线观看| 亚洲综合精品网站| 拍拍拍又黄又爽无挡视频免费| 成人爽a毛片免费| 青青免费在线视频| 色老板亚洲视频免在线观| 亚洲成AV人片在WWW色猫咪| 国产免费无遮挡精品视频 | 中文字幕久久亚洲一区| 成人免费视频一区二区三区| 久久久久久国产精品免费免费男同| MM1313亚洲国产精品| 亚洲国产成人久久77| 亚洲2022国产成人精品无码区 | 亚洲成a人无码av波多野按摩| 久久九九兔免费精品6| 青柠影视在线观看免费高清 | 亚洲国产婷婷综合在线精品| 成人免费一区二区无码视频| 69视频在线观看免费| 久久青草91免费观看| 99在线视频免费观看|