首页 > 解决方案 > 如何为 IMAPFolder.idle 设置超时

问题描述

我正在用 java 实现 Webdriver 测试,包括检查来自 Gmail 的电子邮件。我的代码在这里:

public static void checkNewEmail(String user, String password, String sender, String subject, ArrayList<String> errorList) throws MessagingException {
    boolean result = false;
    Store store = null;
    Properties props = new Properties();
    props.setProperty("mail.store.protocol", "imaps");
    props.setProperty("mail.imaps.ssl.enable", "true");
    props.setProperty("mail.imaps.port", "993");
    props.setProperty("mail.imaps.timeout", "10000");
    props.setProperty("mail.imaps.connectiontimeout", "10000");

    try {

        Session session = Session.getInstance(props, null);
        store = session.getStore();
        store.connect("imap.gmail.com", user, password);
        Folder inbox = store.getFolder("INBOX");
        inbox.open(Folder.READ_ONLY);
        //add listener
        System.out.println("Listening for New message");
        final Message[] email = new Message[1];
        inbox.addMessageCountListener(new MessageCountAdapter() {
            @Override
            public void messagesAdded(MessageCountEvent ev) {
                Message[] messages = ev.getMessages();
                for (Message msg : messages) {
                    //process emails here
                    System.out.println("New message");
                    email[0] = msg;
                }
            }
        });
        ((IMAPFolder) inbox).idle(true);

        if(email[0] != null) {
            if (email[0].getFrom()[0].toString().contains(sender) && email[0].getSubject().contains(subject)) {
                gm.printSuccessMsg("===PASSED: Correct email arrived.");
                result = true;
            } else {
                System.out.println("Incorrect email arrived.");
                System.out.println(email[0].getFrom()[0].toString());
                System.out.println(email[0].getSubject());
                if (store != null) {
                    store.close();
                }
                checkNewEmail(user, password, sender, subject, errorList);
            }
        }

    } catch (FolderClosedException e) {
        e.printStackTrace();
        if (store != null) {
            store.close();
        }

    } finally {
        if(!result)
            errorList.add("Store email");
        if (store != null) {
            store.close();
            System.out.println("Done");
        }
    }
}
}

调试输出:

    DEBUG: setDebug: JavaMail version 1.6.2
DEBUG: getProvider() returning javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle]
DEBUG IMAPS: mail.imap.fetchsize: 16384
DEBUG IMAPS: mail.imap.ignorebodystructuresize: false
DEBUG IMAPS: mail.imap.statuscachetimeout: 1000
DEBUG IMAPS: mail.imap.appendbuffersize: -1
DEBUG IMAPS: mail.imap.minidletime: 10
DEBUG IMAPS: closeFoldersOnStoreFailure
DEBUG IMAPS: trying to connect to host "imap.gmail.com", port 993, isSSL true
* OK Gimap ready for requests from 103.248.166.14 y22mb380918775jaq
A0 CAPABILITY
* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 XYZZY SASL-IR AUTH=XOAUTH2 AUTH=PLAIN AUTH=PLAIN-CLIENTTOKEN AUTH=OAUTHBEARER AUTH=XOAUTH
A0 OK Thats all she wrote! y22mb380918775jaq
DEBUG IMAPS: AUTH: XOAUTH2
DEBUG IMAPS: AUTH: PLAIN
DEBUG IMAPS: AUTH: PLAIN-CLIENTTOKEN
DEBUG IMAPS: AUTH: OAUTHBEARER
DEBUG IMAPS: AUTH: XOAUTH
DEBUG IMAPS: protocolConnect login, host=imap.gmail.com, user=nobia.bada.sigdal@gmail.com, password=<non-null>
DEBUG IMAPS: AUTHENTICATE PLAIN command trace suppressed
DEBUG IMAPS: AUTHENTICATE PLAIN command result: A1 OK nobia.bada.sigdal@gmail.com authenticated (Success)
A2 ENABLE UTF8=ACCEPT
* ENABLED UTF8=ACCEPT
A2 OK Success
DEBUG IMAPS: connection available -- size: 1
A3 EXAMINE INBOX
* FLAGS (\Answered \Flagged \Draft \Deleted \Seen $NotPhishing $Phishing)
* OK [PERMANENTFLAGS ()] Flags permitted.
* OK [UIDVALIDITY 1] UIDs valid.
* 618 EXISTS
* 0 RECENT
* OK [UIDNEXT 817] Predicted next UID.
* OK [HIGHESTMODSEQ 69350]
A3 OK [READ-ONLY] INBOX selected. (Success)
Listening for New message
A4 IDLE
+ idling
DEBUG IMAP: startIdle: set to IDLE
DEBUG IMAP: startIdle: return true
DEBUG IMAP: handleIdle: ignoring socket timeout
DEBUG IMAP: handleIdle: ignoring socket timeout
DEBUG IMAP: handleIdle: ignoring socket timeout
DEBUG IMAP: handleIdle: ignoring socket timeout
DEBUG IMAP: handleIdle: ignoring socket timeout
DEBUG IMAP: handleIdle: ignoring socket timeout
DEBUG IMAP: handleIdle: ignoring socket timeout
DEBUG IMAP: handleIdle: ignoring socket timeout
DEBUG IMAP: handleIdle: ignoring socket timeout
DEBUG IMAP: handleIdle: ignoring socket timeout
DEBUG IMAP: handleIdle: ignoring socket timeout
DEBUG IMAP: handleIdle: ignoring socket timeout
DEBUG IMAP: handleIdle: ignoring socket timeout
....

当有正确的电子邮件到达时,它可以正确地检查和终止侦听器。但是当没有收到邮件时,它会一直等待,并且超时似乎不起作用,它一直挂在DEBUG IMAP: handleIdle: ignoring socket timeout. 有没有人有同样的问题?我尝试从“setProperty”更改为“put”(使用 int 而不是 string),但它仍然是一样的。

标签: selenium-webdriverjakarta-mail

解决方案


IMAPFolder 来源

/*
 * If it was a timeout and no bytes were transferred
 * we ignore it and go back and read again.
 * If the I/O was otherwise interrupted, and no
 * bytes were transferred, we take it as a request
 * to abort the IDLE.
 */

因此设置超时不会导致 IDLE 中止。如果您希望IDLE 中止,请创建另一个线程以等待截止日期并在截止日期到期时调用 close。


推荐阅读