2016-09-26 79 views
0

我試圖從收件箱中獲取未讀電子郵件(Outlook.office365.com),並將讀取的消息複製到另一個文件夾。但是我遇到以下一些問題。如何從Outlook中使用javax.mail pop3獲取未讀電子郵件

  1. 即使我們將電子郵件標記爲 以編程方式閱讀,電子郵件仍會再次被重新讀取。
  2. 無法將電子郵件複製到其他文件夾,即使我們打開收件箱時也是RW(讀取&寫入)模式。

附加我的代碼。

package com.xyz.mail; 

import java.io.FileOutputStream; 
import java.io.InputStream; 
import java.util.Properties; 

import javax.mail.Flags; 
import javax.mail.Folder; 
import javax.mail.Message; 
import javax.mail.Multipart; 
import javax.mail.Part; 
import javax.mail.Session; 
import javax.mail.Store; 
import javax.mail.internet.MimeBodyPart; 
import javax.mail.search.FlagTerm; 

public class ReadEmail { 

    private static final String TRUE = "true"; 
    private static final String MAIL_POP3_HOST = "mail.pop3.host"; 
    private static final String MAIL_POP3_PORT = "mail.pop3.port"; 
    private static final String MAIL_POP3_STARTTLS_ENABLE = "mail.pop3.starttls.enable"; 
    private static final String MAIL_FOLDER_INBOX = "INBOX"; 

    public static void check(String host, String storeType, String user, String password) throws Exception { 

     Store store = null; 
     Folder emailFolder = null; 
     try { 
      Properties properties = new Properties(); 
      properties.put(MAIL_POP3_HOST, host); 
      properties.put(MAIL_POP3_PORT, "995"); 
      properties.put(MAIL_POP3_STARTTLS_ENABLE, TRUE); 
      Session emailSession = Session.getDefaultInstance(properties); 

      // create the POP3 store object and connect with the pop server 
      store = emailSession.getStore(storeType); 

      store.connect(host, user, password); 

      emailFolder = store.getFolder(MAIL_FOLDER_INBOX); 
      emailFolder.open(Folder.READ_WRITE); 

      Message[] messages = emailFolder.search(new FlagTerm(new Flags(Flags.Flag.SEEN), false)); 

      System.out.println("messages.length---" + messages.length); 
      if (messages.length == 0) { 
       System.out.println("No new messages found."); 
      } else { 
       for (int i = 0, len = messages.length; i < len; i++) { 
        Message message = messages[i]; 

        boolean hasAttachments = hasAttachments(message); 
        if (hasAttachments) { 
         System.out.println(
           "Email #" + (i + 1) + " with subject " + message.getSubject() + " has attachments."); 
         readAttachment(message); 
        } else { 
         System.out.println("Email #" + (i + 1) + " with subject " + message.getSubject() 
           + " does not have any attachments."); 
         continue; 
        } 

        Folder copyFolder = store.getFolder("copyData"); 
        if (copyFolder.exists()) { 
         System.out.println("copy messages..."); 
         copyFolder.copyMessages(messages, emailFolder); 
         message.setFlag(Flags.Flag.DELETED, true); 
        } 
       } 
      } 

     } catch (Exception e) { 
      throw new Exception(e); 
     } finally { 
      emailFolder.close(false); 
      store.close(); 
     } 
    } 

    public static void main(String[] args) throws Exception { 

     String host = "outlook.office365.com"; 
     String username = "[email protected]"; 
     String password = "passw0rd{}"; 
     String mailStoreType = "pop3s"; 

     check(host, mailStoreType, username, password); 

    } 

    private static boolean hasAttachments(Message msg) throws Exception { 
     if (msg.isMimeType("multipart/mixed")) { 
      Multipart mp = (Multipart) msg.getContent(); 
      if (mp.getCount() > 1) { 
       return true; 
      } 
     } 

     return false; 
    } 

    public static void readAttachment(Message message) throws Exception { 

     Multipart multiPart = (Multipart) message.getContent(); 
     for (int i = 0; i < multiPart.getCount(); i++) { 
      MimeBodyPart part = (MimeBodyPart) multiPart.getBodyPart(i); 
      if (Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition())) { 
       String destFilePath = "/home/user/Documents/" + part.getFileName(); 
       System.out.println("Email attachement ---- " + destFilePath); 
       FileOutputStream output = new FileOutputStream(destFilePath); 
       InputStream input = part.getInputStream(); 
       byte[] buffer = new byte[4096]; 
       int byteRead; 
       while ((byteRead = input.read(buffer)) != -1) { 
        output.write(buffer, 0, byteRead); 
       } 
       output.close(); 
      } 
     } 
    } 

} 

如何僅提取未讀電子郵件並將電子郵件複製到其他文件夾。

回答

0

變化protocal到IMAP和改變PROT respecitvely ..using POP3,我們只可以訪問收件箱中就是爲什麼你無法將郵件複製到另一個文件夾

+0

是,使用IMAP,POP3不和一定要修復這些[常見的JavaMail錯誤](http://www.oracle.com/technetwork/java/javamail/faq/index.html#commonmistakes)。 –

相關問題