我正在尝试阅读javamail收件箱并执行搜索。为此,我要获取最新的100条消息,然后逐一进行遍历,以查看它们是否包含要搜索的发件人。如果匹配,我将通过getContent()获得其内容。

这是我的javamail代码段:

try {

            Properties properties = new Properties();
            properties.setProperty("mail.store.protocol", "imap");
            properties.put("mail.imaps.starttls.enable", "true");
            properties.put("mail.imap.socketFactory.port", "587");
            System.setProperty("javax.net.debug", "ssl");
            System.out.println("prop " + properties.getProperty("mail.smtp.port"));

            Session session = Session.getDefaultInstance(properties, null);
            // session.setDebug(true);
            Store store = null;
            store = session.getStore("imaps");
            store.connect("imap.gmail.com", username, password);
            Folder inbox;
            inbox = store.getFolder("Inbox");
      /* Others GMail folders :
       * [Gmail]/All Mail   This folder contains all of your Gmail messages.
       * [Gmail]/Drafts     Your drafts.
       * [Gmail]/Sent Mail  Messages you sent to other people.
       * [Gmail]/Spam       Messages marked as spam.
       * [Gmail]/Starred    Starred messages.
       * [Gmail]/Trash      Messages deleted from Gmail.
       */
            inbox.open(Folder.READ_WRITE);

            Message msgs[] = inbox.getMessages(inbox.getMessageCount() - lastHistory, inbox.getMessageCount());
            System.out.println("MSgs.length " + msgs.length);
            ArrayList<Message> aList = new ArrayList<Message>();
            appendTextToConsole("Searching for appropriate messages!!");
            for (int ii = msgs.length - 1; ii >= 0; ii--) {
                Message msg = msgs[ii];
                Address[] in = msg.getFrom();
                String sender = InternetAddress.toString(in);
                System.out.println((++index) + "Sender: " + sender);
                boolean read = msg.isSet(Flags.Flag.SEEN);
                if (sender.contains(googleId) && !read) {
//This line below gives FolderClosedException sporadically

                    Object content = msg.getContent();
                    if (content instanceof Multipart) {
                        Multipart mp = (Multipart) content;
                        for (int i = 0; i < mp.getCount(); i++) {
                            BodyPart bp = mp.getBodyPart(i);
                            if (Pattern
                                    .compile(Pattern.quote("text/html"),
                                            Pattern.CASE_INSENSITIVE)
                                    .matcher(bp.getContentType()).find()) {
                                // found html part
                                String html = (String) bp.getContent();
                                Element element = Jsoup.parse(html);
                                List<Element> anchors = element.getElementsByTag("a");
                                for (Element e : anchors) {
                                    if (e.attr("href").startsWith("https://www.google.com/url?rct=j&sa=t&url=")
                                            && !e.attr("style").equalsIgnoreCase("text-decoration:none")) {
                                        String url = e.attr("href");
                                        String title = e.text();
                                        String agency = e.parent().parent().child(1).child(0).child(0).text();
                                        String message = e.parent().parent().child(1).child(0).child(1).text();
                                        String flagUrl = e.parent().parent().child(1).child(1).child(0).child(0).child(3).child(0).attr("href");
                                        System.out.println("URL: " + url);
                                        System.out.println("Title: " + title);
                                        System.out.println("agency: " + agency);
                                        System.out.println("Message: " + message);
                                        System.out.println("flagURL: " + flagUrl);
                                        AbstractMessage ams = new AbstractMessage(url, title, agency, message, flagUrl);
                                        aMsgs.add(ams);
                                    }
                                }
                                //System.out.println((String) bp.getContent());
                            } else {
                                // some other bodypart...
                            }
                        }
                        try {
                            inbox.close(true);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
            appendTextToConsole("Done searching for appropriate messages!!");
        } catch (Exception mex) {
            appendTextToConsole(mex.getMessage());
            mex.printStackTrace();
        }


但是最令人恼火的是,在获取了一些消息之后,由于未知原因,偶尔会抛出javax.mail.FolderClosedException。现在我的问题是,如何处理这种情况?使用javamail制作的理想邮件客户端又如何处理呢?

最佳答案

打开session debugging,您可能会得到更多有关正在发生的事情的线索。

请注意,如果您不使用服务器,它将关闭连接。
当然,各种网络故障都是可能的。

关于java - 使用javamail阅读Gmail电子邮件时偶尔获得javax.mail.FolderClosedException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25095261/

10-13 01:46