簡體   English   中英

JavaMail:Folder.hasNewMessages()和.getMessageCount()似乎無法正常工作

[英]JavaMail : Folder.hasNewMessages() and .getMessageCount() don't seem to work properly

我有這個簡單的代碼來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;

}

getMessageCount()和hasNewMessages()都不起作用,因為第一個總是返回相同數量的消息,第二個總是返回false。 我做錯了什么? 我不想使用messagelistener,因為這段代碼將在性能非常低的機器人上運行。感謝大家

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM