【1】功能實現:僅得到郵件頭而不下載整個郵件php
初次進入郵箱的時候,若是郵件包含的附件較多的狀況下,顯示加載會很慢html
用戶體驗很差,通常設計爲僅僅加載郵件頭java
File file = new File("uids.txt"); UIDStore uids = new UIDStore(); uids.load(file); URLName url = new URLName("pop3", parameters[0], 110, "", parameters[1], parameters[2]); Session session = Session.getDefaultInstance(System.getProperties(),null); Store store = session.getStore(url); store.connect(); POP3Folder inbox = (POP3Folder)store.getFolder("INBOX"); inbox.open(Folder.READ_WRITE); //重點在這裏 開始 FetchProfile profile = new FetchProfile(); profile.add(UIDFolder.FetchProfileItem.UID); Message[] messages = inbox.getMessages(); inbox.fetch(messages,profile); //重點在這裏 結束 for(int i = 0;i < messages.length;i++) { String uid = inbox.getUID(messages[i]); if(uids.isNew(uid)) { System.out.print(i); System.out.print(". "); System.out.println(inbox.getMessage(i + 1).getSubject()); } } System.out.println("Done."); uids.store(file);
參考網站:session
http://www.developer.com/java/other/article.php/3092171/JavaMail-More-Efficiently.htm函數
【2】工程Demo代碼參考fetch
Message[] messages = folder.getMessages(startPosition, endPosition); // // add by fnst for i信4.0 郵箱 on 2015-02-10 start // 從新設計,僅僅只是得到郵件頭部信息【郵件地址+郵件時間+郵件是否包含附件+郵件標題】 FetchProfile fp = new FetchProfile(); fp.add(FetchProfile.Item.ENVELOPE); folder.fetch(messages, fp); for (int i = 0; (i < messages.length); i++) { String uid = inbox.getUID(messages[i]); if (EmailDao.getEmailInfoById(uid) == null) { try { saveEmailMsg2DB(handler, inbox, messages, i,startPosition); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
【3】扯點別的網站
1> getContent()的使用注意ui
messgaes.getContent()返回一個Multipart對象,一個Multipart對象包含一個或多個BodyPart對象,來組成郵件的正文部分(包括附件)。url
可是... 使用getContent()會去遍歷整封郵件,會將整個郵件下載下來,效率不好spa
2> 郵件頭與郵件體
郵件頭包含了發件人、收件人、主題、時間、MIME版本、郵件內容的類型等重要信息。每條信息稱爲一個域,由域名後加「: 」和信息內容構成,能夠是一行,較長的也能夠佔用多行。域的首行必須「頂頭」寫,即左邊不能有空白字符(空格和製表符);續行則必須以空白字符打頭,且第一個空白字符不是信息自己固有的,解碼時要過濾掉。
郵件體包含郵件的內容,它的類型由郵件頭的「Content-Type」域指出。常見的簡單類型有text/plain(純文本)和text/html(超文本)。有時也會出現的multipart類型,是MIME郵件的精髓。郵件體被分爲多個段,每一個段又包含段頭和段體兩部分,這兩部分之間也以空行分隔。常見的multipart類型有三種:multipart/mixed, multipart/related和multipart/alternative。
multipart/mixed:附件。
multipart/related:內嵌資源。
multipart/alternative:純文本與超文本共存。
3> 構造函數
mpRoot = new MimeMultipart("alternative"); mpRoot = new MimeMultipart("related"); mpRoot = new MimeMultipart();【默認是mixed】
4> 很大可能性會遇到的一個報錯處理代碼
message.setContent(mpRoot); MailcapCommandMap mc = (MailcapCommandMap) CommandMap .getDefaultCommandMap(); mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html"); mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml"); mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain"); mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed"); mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822"); CommandMap.setDefaultCommandMap(mc); Transport.send(message); handler.obtainMessage(1).sendToTarget();
參考
http://blog.sina.com.cn/s/blog_6d3c1ec601010bzd.html