javax.mail 中的Folder.create() 在什么情况下返回 false?

发布于 2024-09-11 15:16:18 字数 1036 浏览 11 评论 0原文

我正在尝试让电子邮件处理 Java 应用程序将所有已处理的邮件从 IMAP 收件箱移动到子文件夹。如果该子文件夹不存在,则应创建它。最后一点是行不通的。

代码片段为:

private void _backupMessage(Message msg, Folder folder, String sBackupFolderName) throws MessagingException
{
    Folder backupFolder = folder.getFolder(sBackupFolderName);
    if (!backupFolder.exists()) {
        boolean f = backupFolder.create(Folder.HOLDS_FOLDERS & Folder.HOLDS_MESSAGES);
        if (!f) {
            this._triggerFaultEvent(new RuntimeException("Could not create backup folder."));
        }
    }
    backupFolder.open(Folder.READ_WRITE);
    folder.copyMessages(new Message[] { msg }, backupFolder);
    backupFolder.close(true);
}

相应的 Javadoc 为 这里,但它实际上什么也没说,只是如果 create() 返回 false,则不会创建该文件夹(令人惊讶,令人惊讶)。

我能够使用相同的帐户使用 Thunderbird 创建该文件夹。

我的电子邮件服务器(Postfix)没有显示任何日志条目,除非它找不到新文件夹。在相应的单元测试中,模拟电子邮件服务器(GreenMail)要么工作,要么忽略该命令,无论如何,测试都会通过。

I'm trying to have an email-processing Java application move all processed mails from an IMAP inbox to a subfolder. If that subfolder does not exist, it should create it. This last bit is what doesn't work.

The code snippet is:

private void _backupMessage(Message msg, Folder folder, String sBackupFolderName) throws MessagingException
{
    Folder backupFolder = folder.getFolder(sBackupFolderName);
    if (!backupFolder.exists()) {
        boolean f = backupFolder.create(Folder.HOLDS_FOLDERS & Folder.HOLDS_MESSAGES);
        if (!f) {
            this._triggerFaultEvent(new RuntimeException("Could not create backup folder."));
        }
    }
    backupFolder.open(Folder.READ_WRITE);
    folder.copyMessages(new Message[] { msg }, backupFolder);
    backupFolder.close(true);
}

The corrsponding Javadoc is here, but it really doesn't say anything except that if create() returns false, the folder wasn't created (surprise, surprise).

I was able to create the folder using Thunderbird with the same account.

My email server (Postfix) didn't show any log entries, except where it couldn't find the new folder. In the corresponding unit test, the mock email server (GreenMail) either works or ignores the command, in any case, the test passes.

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

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

发布评论

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

评论(2

假情假意假温柔 2024-09-18 15:16:18

首先,Postfix不是IMAP服务器。应该有另一个程序为您提供 IMAP 服务,并且可以在该程序中检查日志,而不是 Postfix。

何时返回 false 取决于实现。 SUN 的 Javamail 实现将 返回 false

  1. 如果创建文件夹的 IMAP 命令失败,或者
  2. IMAP 命令成功,但该文件夹仍然不存在,如使用 contains() 检查,则
  3. ,或者如果您要求一个可以容纳子文件夹的文件夹,它还会检查创建的文件夹没有 \Noinferiors 标志。这是因为某些 IMAP 服务器不支持同时包含邮件和子文件夹的文件夹。

我建议您获取实现的源代码并使用调试器逐步完成它。这可能是找出到底出了什么问题的最快方法。

First of all, Postfix is not an IMAP server. There should be another program serving IMAP for you and that is the place to check the logs, not Postfix.

When exactly false is returned depends on implementation. SUN's Javamail implementation will return false if

  1. IMAP command to create a folder has failed, OR
  2. IMAP command was successful, but the folder still did not exist, as checked with exists(), OR
  3. If you ask for a folder which can hold subfolders, it will also check that the created folder doesn't have \Noinferiors flag. This is because some IMAP servers don't support folders with both messages and subfolders.

I suggest you get a source for your implementation and step through it with a debugger. This is probably the quickest way to find out what exactly is wrong.

心舞飞扬 2024-09-18 15:16:18

注意:

Folder.HOLDS_MESSAGES == 1

Folder.HOLDS_FOLDERS == 2

Folder.HOLDS_FOLDERS & Folder.HOLDS_MESSAGES == 0(始终)

您可以尝试:Folder.HOLDS_FOLDERS | Folder.HOLDS_MESSAGES 等于 3

Beware:

Folder.HOLDS_MESSAGES == 1

Folder.HOLDS_FOLDERS == 2

Folder.HOLDS_FOLDERS & Folder.HOLDS_MESSAGES == 0 (always)

You may try: Folder.HOLDS_FOLDERS | Folder.HOLDS_MESSAGES which equals 3

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