javax.mail 中的Folder.create() 在什么情况下返回 false?
我正在尝试让电子邮件处理 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,Postfix不是IMAP服务器。应该有另一个程序为您提供 IMAP 服务,并且可以在该程序中检查日志,而不是 Postfix。
何时返回 false 取决于实现。 SUN 的 Javamail 实现将 返回 false
\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\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.
注意:
Folder.HOLDS_MESSAGES == 1
Folder.HOLDS_FOLDERS == 2
Folder.HOLDS_FOLDERS & Folder.HOLDS_MESSAGES == 0
(始终)您可以尝试:
Folder.HOLDS_FOLDERS | Folder.HOLDS_MESSAGES
等于 3Beware:
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