2010-10-11 80 views
1

我知道如何從INBOX文件夾中收回郵件......但是現在我想從SENT ITEMS文件夾中檢索郵件......我正在使用imap來檢索數據... 讓我知道我應該在這個函數中傳遞什麼參數從SENT ITEMS文件夾中獲取郵件 Folder folder=store.getFolder("inbox");我應該將收件箱更改爲一些stirng我想知道該字符串...閱讀從郵件服務器發送的郵件

回答

2

我發現我的問題的解決方案.... 我用這個代碼,列出從郵件服務器 的文件夾和傳遞的GetFolder這些值()函數...它的正常工作..

Folder[] folderList = store.getDefaultFolder().list(); 
     for (int i = 0; i < folderList.length; i++) { 
      System.out.println(folderList[i].getFullName()); 
     } 
+0

的文件夾@ Kandhasamy:尼斯卡ndhasamy ............. – 2010-10-11 10:39:55

3

這裏沒有標準名稱。 IMAP spec要求將收件箱稱爲「INBOX」,但沒有特別定義其他文件夾。它畢竟只是一個文件夾的名稱 - 一些提供商將使用「已發送」,一些將使用「已發送郵件」,您甚至可能會看到其他一些變體。

我建議列出服務器知道的文件夾,然後從那裏選擇合適的文件夾(交互式地或者在運行無頭的情況下爲名字中的「發送」)。總體來說更好的選擇可能是使其成爲一個可配置的參數(如果您的應用程序已經有一個屬性文件)。

當然,如果這是一次性項目,您可以硬編碼特定服務器的值。但是,如果你想正確地做到這一點,你需要靈活。

+0

謝謝安傑伊·多伊爾......你知道,我們怎麼能列出服務器知道 – Kandha 2010-10-11 09:35:39

0

此代碼將檢索所有郵件,將打印內容,並存儲在本地,如果有任何附件

public class MailReader { 
     Folder inbox; 
     public MailReader() { 
      Properties props = System.getProperties(); 
      props.setProperty("mail.store.protocol", "imaps"); 
      try {   
       Session session = Session.getDefaultInstance(props, null); 
       Store store = session.getStore("imaps"); 
       store.connect("imap.gmail.com", "username", 
         "password"); 
       /* Mention the folder name which you want to read. */ 
       inbox = store.getFolder("Inbox"); 
       System.out.println("No of Unread Messages : " 
         + inbox.getMessageCount() + " " 
         + inbox.getUnreadMessageCount()); 

       /* Open the inbox using store. */ 
       inbox.open(Folder.READ_ONLY); 

       /* 
       * Get the messages which is unread in the Inbox Message messages[] 
       * = inbox.search(new FlagTerm(new Flags(Flag.SEEN), false)); 
       */ 
       Message messages[] = inbox.getMessages(); 
       /* Use a suitable FetchProfile */ 
       FetchProfile fp = new FetchProfile(); 
       fp.add(FetchProfile.Item.ENVELOPE); 
       fp.add(FetchProfile.Item.CONTENT_INFO); 
       inbox.fetch(messages, fp); 

       try { 
        printAllMessages(messages); 
        inbox.close(true); 
        store.close(); 
       } catch (Exception ex) { 
        System.out.println("Exception arise at the time of read mail"); 
        ex.printStackTrace(); 
       } 
      } catch (NoSuchProviderException e) { 
       e.printStackTrace(); 
       System.exit(1); 
      } catch (MessagingException e) { 
       e.printStackTrace(); 
       System.exit(2); 
      } 
     } 

     public void printAllMessages(Message[] msgs) throws Exception { 
      for (int i = 0; i < msgs.length; i++) { 
       System.out.println("MESSAGE #" + (i + 1) + ":"); 
       printEnvelope(msgs[i]); 
      } 
     } 

     /* Print the envelope(FromAddress,ReceivedDate,Subject) */ 
     public void printEnvelope(Message message) throws Exception { 
      Address[] a; 
      // FROM 
      if ((a = message.getFrom()) != null) { 
       for (int j = 0; j < a.length; j++) { 
        System.out.println("FROM: " + a[j].toString()); 
       } 
      } 
      // TO 
      if ((a = message.getRecipients(Message.RecipientType.TO)) != null) { 
       for (int j = 0; j < a.length; j++) { 
        System.out.println("TO: " + a[j].toString()); 
       } 
      } 
      String subject = message.getSubject(); 
      Date receivedDate = message.getReceivedDate(); 
      String content = message.getContent().toString(); 
      System.out.println("Subject : " + subject); 
      System.out.println("Received Date : " + receivedDate.toString()); 
      System.out.println("Content : " + content); 
      getContent(message); 
     } 

     public void getContent(Message msg) { 
      try { 
       String contentType = msg.getContentType(); 
       System.out.println("Content Type : " + contentType); 
       Multipart mp = (Multipart) msg.getContent(); 
       int count = mp.getCount(); 
       for (int i = 0; i < count; i++) { 
        dumpPart(mp.getBodyPart(i)); 
       } 
      } catch (Exception ex) { 
       System.out.println("Exception arise at get Content"); 
       ex.printStackTrace(); 
      } 
     } 

     public void dumpPart(Part p) throws Exception { 
      // Dump input stream .. 
      if (p.getFileName() == null) { 
       return; 
      } 
      System.out.println("filename:" + p.getFileName()); 
      System.out.println(p.ATTACHMENT); 
      InputStream is = p.getInputStream(); 
      File file = new File(p.getFileName()); 
      FileOutputStream fout = null; 
      fout = new FileOutputStream(p.getFileName()); 
      // If "is" is not already buffered, wrap a BufferedInputStream 
      // around it. 
      if (!(is instanceof BufferedInputStream)) { 
       is = new BufferedInputStream(is); 
      } 
      int c; 
      System.out.println("Message : "); 
      while ((c = is.read()) != -1) { 
       fout.write(c); 
      } 
      if (fout != null) { 
       fout.close(); 
      } 
     } 

     public static void main(String args[]) { 
      new MailReader(); 
     } 
    } 
+0

參考:1.http://www.vipan.com/htdocs/javamail.html 2.http://metoojava.wordpress.com/2010/03/ 21/java的碼到接收郵件-使用-javamailapi / – Kandha 2010-11-29 09:46:17

相關問題