如何为IMAPFolder.idle设置超时

时间:2020-06-15 02:53:12

标签: selenium-webdriver jakarta-mail

我正在用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而不是字符串),但仍然相同。

1 个答案:

答案 0 :(得分:0)

来自IMAPFolder source

/*
 * 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 to abort创建另一个线程来等待截止日期,并在截止日期到期时调用close。