2012-10-24 1 views
0

작동하지 않는 것JavaMail에 : Folder.hasNewMessages()와 .getMessageCount는() 나는 perdiodically POP3 서버에 액세스하고받은 편지함 폴더에 새 메시지가 있는지 확인이 간단한 코드가 제대로

public static void main(String[] args) { 


    try { 

     String storeType = "pop3"; 

     String host = "..."; 
     int port = ...; 
     String user = "..."; 
     String psw = "..."; 

     // 
     int delay = 1; 
     long mins = 200 * 60 * delay; 
     firstAccess = true; 


     Properties properties = new Properties(); 
     properties.put("mail.pop3.host", host); 
     properties.put("mail.pop3.user", user); 
     properties.put("mail.pop3.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 
     properties.put("mail.pop3.port", port); 


     RomasAuthenticator r = new RomasAuthenticator(user, psw); 

     Session session = Session.getDefaultInstance(properties, r); 

     POP3Store emailStore = (POP3Store) session.getStore(storeType); 
     emailStore.connect(host, port, user, psw); 

     Folder emailFolder = emailStore.getFolder("INBOX"); 
     emailFolder.open(Folder.READ_ONLY); 

     while (true) { 
      if (checkInbox(emailFolder)) { 
       //prepara la notifica per il voice_service 
       System.out.println("mes!"); 
      } 
      firstAccess = false; 
      Thread.sleep(mins); 
     } 




    } catch (Exception ex) { 
     //return null; 
    } 
} 


private static boolean checkInbox(Folder inbox_folder) throws MessagingException, IOException { 
    System.out.println("checking"); 
    boolean res = false; 

    try { 

     int email_actual_count = inbox_folder.getMessageCount(); 

     if (email_actual_count > email_prev_count /*or hasNewMessages()*/) { 
      if (!firstAccess) { 
       res = true; 
      } 
     } 

     //bisogna aggiornare comunque email_count in caso siano stati cancellati messaggi 
     email_prev_count = email_actual_count; 

    } 

    catch (Exception e) { 
     e.printStackTrace(); 
    } 

    return res; 

} 

첫 번째 메시지는 항상 같은 수의 메시지를 반환하고 두 번째 메시지는 항상 false를 반환하기 때문에 getMessageCount() 및 hasNewMessages()가 작동하지 않습니다. 내가 뭐 잘못한거야? hasNewMessages()는 항상 false를 반환하는 이유 내가 ..이 코드는 정말 낮은 성능을 가진 로봇에서 실행하기 때문에 MessageListener를를 사용하는 모두 감사합니다

답변

2
+0

을 원하지 않는 자주 묻는 질문는 설명한다. messagecount()는 어떨까요? 왜 업데이트되지 않습니까? – Filippo

+1

POP3 프로토콜은 새 메시지가 도착했음을 클라이언트에 알릴 방법이 없으므로 폴더를 열 때 업데이트되지 않습니다. 가능한 경우 POP3 대신 IMAP을 사용하십시오. –