JavaMail - 无法folder.open()

发布于 2024-10-31 19:06:59 字数 2489 浏览 3 评论 0原文

正如标题所示,当尝试执行folder.open() 时,它会失败,但不会抛出错误,因此很难找到原因。在我的调试控制台中,确实出现了以下错误,但它可能/可能不相关(这只在folder.open()断点后按resume后显示)。

我正在使用 Android 版 JavaMail API 进行开发。 IMAP 服务器一切正常,但我还需要能够连接到 POP3 邮件服务器。连接到的商店是 GMAIL,并且我的 GMAIL 帐户上的所有必要设置均已更改。

04-12 13:22:26.682: INFO/dalvikvm(436): Ljava/lang/IllegalStateException;: Folder is not Open
04-12 13:22:26.682: INFO/dalvikvm(436):     at com.sun.mail.pop3.POP3Folder.checkOpen(POP3Folder.java:512)
04-12 13:22:26.682: INFO/dalvikvm(436):     at com.sun.mail.pop3.POP3Folder.close(POP3Folder.java:227)
04-12 13:22:26.682: INFO/dalvikvm(436):     at com.sun.mail.pop3.POP3Folder.finalize(POP3Folder.java:506)
04-12 13:22:26.682: INFO/dalvikvm(436):     at dalvik.system.NativeStart.run(Native Method)

pop3的连接方法如下:

String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";

        Properties pop3Props = new Properties();

        pop3Props.setProperty("mail.pop3.socketFactory.class", SSL_FACTORY);
        pop3Props.setProperty("mail.pop3.socketFactory.fallback", "false");
        pop3Props.setProperty("mail.pop3.port",  "995");
        pop3Props.setProperty("mail.pop3.socketFactory.port", "995");

        URLName url = new URLName("pop3", "pop.gmail.com", 995, "",
                "******@googlemail.com", "*****");

        //session = Session.getInstance(pop3Props, null);
        Session session = Session.getInstance(pop3Props, new javax.mail.Authenticator() 
        {   
            protected PasswordAuthentication getPasswordAuthentication() 
            {
                return new PasswordAuthentication("***********", "*****");
            }
        });

        session.setDebug(true);

        store = new POP3SSLStore(session, url);
        store.connect();

连接商店的方法如下(遇到问题的地方):

folders = store.getFolder("INBOX");
        //Folder folder = store.getFolder("INBOX");
        if (folders.isOpen()) 
        {
            if ((folders.getMode() & Folder.READ_WRITE) != 0) 
            {
                folders.close(false);
                folders.open(Folder.READ_ONLY);
            }
        } 
        else 
        {
            try
            {
                folders.open(Folder.READ_ONLY);
            }
            catch (Exception e)
            {
                Log.i("Folder Opening,", e.toString());
            }
        }

任何想法都会有帮助! Session已经调试完毕,没有发现任何问题。如果您需要更多信息来帮助您,请随时询问!我确信我不是唯一遇到这个问题的人。

谢谢! 里斯

As the title says, when attempting to do folder.open() it fails, doesn't throw an error though so it is proving hard to find a reason. In my debug console the following error does appear but it may/may not be related (This only shows up after pressing resume after the folder.open() breakpoint).

I am using the JavaMail API for Android for development. Everything was working fine for IMAP servers but I need to be able to connect to POP3 mail servers also. The store being connected to is GMAIL and all the neccessary settings have been changed on my GMAIL account.

04-12 13:22:26.682: INFO/dalvikvm(436): Ljava/lang/IllegalStateException;: Folder is not Open
04-12 13:22:26.682: INFO/dalvikvm(436):     at com.sun.mail.pop3.POP3Folder.checkOpen(POP3Folder.java:512)
04-12 13:22:26.682: INFO/dalvikvm(436):     at com.sun.mail.pop3.POP3Folder.close(POP3Folder.java:227)
04-12 13:22:26.682: INFO/dalvikvm(436):     at com.sun.mail.pop3.POP3Folder.finalize(POP3Folder.java:506)
04-12 13:22:26.682: INFO/dalvikvm(436):     at dalvik.system.NativeStart.run(Native Method)

The connection method for pop3 is as follows:

String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";

        Properties pop3Props = new Properties();

        pop3Props.setProperty("mail.pop3.socketFactory.class", SSL_FACTORY);
        pop3Props.setProperty("mail.pop3.socketFactory.fallback", "false");
        pop3Props.setProperty("mail.pop3.port",  "995");
        pop3Props.setProperty("mail.pop3.socketFactory.port", "995");

        URLName url = new URLName("pop3", "pop.gmail.com", 995, "",
                "******@googlemail.com", "*****");

        //session = Session.getInstance(pop3Props, null);
        Session session = Session.getInstance(pop3Props, new javax.mail.Authenticator() 
        {   
            protected PasswordAuthentication getPasswordAuthentication() 
            {
                return new PasswordAuthentication("***********", "*****");
            }
        });

        session.setDebug(true);

        store = new POP3SSLStore(session, url);
        store.connect();

The method for connecting to a store is as follow (Where the problems are being encountered):

folders = store.getFolder("INBOX");
        //Folder folder = store.getFolder("INBOX");
        if (folders.isOpen()) 
        {
            if ((folders.getMode() & Folder.READ_WRITE) != 0) 
            {
                folders.close(false);
                folders.open(Folder.READ_ONLY);
            }
        } 
        else 
        {
            try
            {
                folders.open(Folder.READ_ONLY);
            }
            catch (Exception e)
            {
                Log.i("Folder Opening,", e.toString());
            }
        }

Any ideas would be helpful! Session has been debugged and no problems are apparent. If any more information is needed for you to assist do not hesitate to ask! I'm sure I'm not the only person getting this problem.

Thanks!
Rhys

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

呆头 2024-11-07 19:06:59

所以,我们也遇到了这个问题。

我查看了触发异常的 POP3Folder 的实现。

似乎 POP3Folder 正在重写 finalize() 以便在对象消失之前执行一些清理操作。 finalize() 覆盖调用 close() 来检查文件夹是否打开,如果没有打开,您看到的 IllegalStateException 是抛出。

这就是为什么您可能会偶尔看到它;当 GC 来查看您的对象时,就会发生这种情况,此时,该文件夹可能已经关闭,因此当 finalize() 调用时close(),检查文件夹是否打开会抛出异常。

现在,我无法判断这是聪明还是愚蠢。如果 close() 忽略文件夹已经关闭并继续移动的事实,而不是抛出异常,那么代码似乎会更好。

POP3Folder 采用参数化构造函数。不幸的是,它不是公共的或受保护的,因此您不能简单地从它派生并重写 close() 方法,从而删除对文件夹是否打开的检查。

但是,由于异常调用堆栈源自 finalize(),因此它应该可以减少单独留下的危险,因为从 finalize() 抛出的任何异常通常只会停止该对象的终结过程(垃圾收集),而不会造成更多损坏。

不过,很想看到一个适当的解决方案。

So, we came across this issue as well.

I took a look at the implementation of POP3Folder where that exception is being triggered.

It seems that POP3Folder is overriding finalize() in order to perform some cleanup before the object goes out of existence. The finalize() override calls close() which checks if the folder is open, and if it isn't, the IllegalStateException you see is thrown.

This is why you may be seeing it sporadically; it happens when the GC comes along to take a look at your object, and at that point, the folder may have been closed already, so by the time finalize() calls close(), the check to see if the folder is open will throw the exception.

Now, I can't speak to whether this is smart or dumb. It does seem that the code would be better if close() would just ignore the fact that the folder is already closed and move along, instead of throwing an exception.

POP3Folder takes a parameterized constructor. Unfortunately, it isn't public or protected, so you can't simply derive from it and override the close() method, removing the check for whether the folder is open.

But, since the exception call stack originates from finalize(), it should make it less of a danger to leave alone, since any exception thrown from finalize() will typically only halt the finalization process (garbage collection) for that object, without causing much more damage.

Would love to see a proper solution to this, though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文