【问题标题】:Sporadically getting javax.mail.FolderClosedException while reading gmail emails using javamail使用 javamail 阅读 gmail 电子邮件时偶尔出现 javax.mail.FolderClosedException
【发布时间】:2014-08-02 12:57:50
【问题描述】:

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

这是我的 javamail 代码 sn-p:

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制作的理想邮件客户端又是如何处理的呢?

【问题讨论】:

  • Gmail 就是这样。真正的邮件客户端重新连接到 gmail 并从中断处继续(而不是从头开始)。

标签: java email gmail jakarta-mail imap


【解决方案1】:

打开session debugging,你可能会得到更多关于发生了什么的线索。

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

【讨论】:

  • 谢谢!我一定会试试的。
猜你喜欢
  • 1970-01-01
  • 2019-02-23
  • 2015-07-30
  • 2012-11-03
  • 2012-12-28
  • 1970-01-01
  • 1970-01-01
  • 2016-02-20
  • 1970-01-01
相关资源
最近更新 更多