0

我有这个简单的代码来定期访问 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。我做错了什么?我不想使用消息侦听器,因为此代码将在性能非常低的机器人上运行.. 谢谢大家

4

1 回答 1

2

JavaMail FAQ 解释了为什么这些功能不适用于 POP3 协议。

于 2012-10-24T22:01:16.650 回答