无法使用 javax.mail 从 google 获取草稿、垃圾邮件和垃圾文件夹中的电子邮件

发布于 2024-12-02 20:03:25 字数 1267 浏览 1 评论 0原文

我想从 gmail 的所有文件夹中获取电子邮件。 我从收件箱、已发送文件夹和其他文件夹中成功收到电子邮件。 但是,当我尝试从草稿、垃圾邮件和垃圾箱中获取它时,出现异常:

09:51:45,622 ERROR MailRetriever.[main]getNoFlaggedMails:142 - Can't get messages: javax.mail.MessagingException: connection failure
javax.mail.MessagingException: connection failure
at com.sun.mail.imap.IMAPStore.getProtocol(IMAPStore.java:742)
at com.sun.mail.imap.IMAPFolder.open(IMAPFolder.java:910)
at ua.com.stormlabs.gap.gmail.MailRetriever.getNoFlaggedMails(MailRetriever.java:133)
at ua.com.stormlabs.gap.gmail.GMailServiceProcessor.processFolder(GMailServiceProcessor.java:95)
at ua.com.stormlabs.gap.gmail.GMailServiceProcessor.start(GMailServiceProcessor.java:80)
at ua.com.stormlabs.gap.gmail.GMailGapApp.main(GMailGapApp.java:21)

这是用于检索邮件的代码:

Folder folder = imapSslStore.getFolder(folderName);
folder.open(Folder.READ_WRITE);
openedFolders.put(folderName, folder);

Flags searchFlags = new Flags(Flags.Flag.USER);
searchFlags.add(FLAG_PREFIX + READ_BY_GAP_FLAG);
Message[] messages = folder.search(new FlagTerm(searchFlags, false));
log.debug("Messages list retrieved: " + messages.length);
return messages;

我尝试打开但未成功的文件夹名称:

[Gmail]/Drafts
[Gmail]/Spam
[Gmail]/Trash

I want to get emails from all folders from gmail.
From inbox, sent and other folders I receive emails successfully.
But when I try to get it from Drafts, Spam and Trash I get an exception:

09:51:45,622 ERROR MailRetriever.[main]getNoFlaggedMails:142 - Can't get messages: javax.mail.MessagingException: connection failure
javax.mail.MessagingException: connection failure
at com.sun.mail.imap.IMAPStore.getProtocol(IMAPStore.java:742)
at com.sun.mail.imap.IMAPFolder.open(IMAPFolder.java:910)
at ua.com.stormlabs.gap.gmail.MailRetriever.getNoFlaggedMails(MailRetriever.java:133)
at ua.com.stormlabs.gap.gmail.GMailServiceProcessor.processFolder(GMailServiceProcessor.java:95)
at ua.com.stormlabs.gap.gmail.GMailServiceProcessor.start(GMailServiceProcessor.java:80)
at ua.com.stormlabs.gap.gmail.GMailGapApp.main(GMailGapApp.java:21)

This is code for retrieving mails:

Folder folder = imapSslStore.getFolder(folderName);
folder.open(Folder.READ_WRITE);
openedFolders.put(folderName, folder);

Flags searchFlags = new Flags(Flags.Flag.USER);
searchFlags.add(FLAG_PREFIX + READ_BY_GAP_FLAG);
Message[] messages = folder.search(new FlagTerm(searchFlags, false));
log.debug("Messages list retrieved: " + messages.length);
return messages;

Folder names I try to open unsuccessfully:

[Gmail]/Drafts
[Gmail]/Spam
[Gmail]/Trash

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

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

发布评论

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

评论(5

孤檠 2024-12-09 20:03:25

我发现我的 Gmail 帐户中的垃圾文件夹名为“[Google Mail]/Bin”等,而不是“Gmail]/Trash”。

我在英国,当我第一次打开 Gmail 帐户时,它确实是[电子邮件受保护] 不是我@gmail.com。

希望这有帮助。

I have found in my Gmail account the trash folder is named "[Google Mail]/Bin" etc. and not "Gmail]/Trash".

I am in the UK and when I first opened my Gmail account it was indeed [email protected] not me @gmail.com.

Hope this helps.

鸩远一方 2024-12-09 20:03:25

最简单的答案是草稿、垃圾邮件和垃圾邮件不是文件夹,而是 Google Mail 中的标签。我正在谷歌搜索来验证我的答案。


更新:经过一些谷歌搜索,我发现了 SO Post 其中指出 [Gmail]/* 文件夹是不可选择的文件夹。

The simplest answer is that Draft, Spam and Trash are not folders but Labels in Google Mail. I am doing a Google search to validate my answer.


Update: After some Google search, I have found a SO Post which states that the [Gmail]/* folders are non-selectable folders.

一页 2024-12-09 20:03:25
Folder[] f = store.getDefaultFolder().list();
for(Folder fd:f){
    Folder t[]=fd.list();

      System.out.println("-------"+fd.getName()+"------");
    for(Folder f1:t)
        System.out.println("->"+f1.getName());

}
Folder[] f = store.getDefaultFolder().list();
for(Folder fd:f){
    Folder t[]=fd.list();

      System.out.println("-------"+fd.getName()+"------");
    for(Folder f1:t)
        System.out.println("->"+f1.getName());

}
纸伞微斜 2024-12-09 20:03:25

以下文件夹名称:

[Gmail]/Spam

非常适合我。这是我经常使用 Javamail 读取垃圾邮件文件夹的代码:

private void readFolder(int max, boolean deletes, List<Message> result, Store store, String folderName) throws MessagingException, IOException {
    Folder folder = null;
    try {
        folder = store.getFolder(folderName);
        folder.open(Folder.READ_WRITE);
        Message[] messages = folder.getMessages();
        for (int i = 0; i < messages.length && result.size() < max; i++) {
            Message message = messages[i];
            if (deletes && (!message.getFlags().contains(Flag.DELETED))) {
                message.setFlag(Flag.DELETED, true);
            }
            result.add(message);
        }
    } finally {
        if (folder != null) {
            try {
                folder.close(true);
            } catch (Exception e) {
            }
        }
    }
}

The following folder name :

[Gmail]/Spam

works perfectly for me. Here is my code I often use to read the spam folder using Javamail :

private void readFolder(int max, boolean deletes, List<Message> result, Store store, String folderName) throws MessagingException, IOException {
    Folder folder = null;
    try {
        folder = store.getFolder(folderName);
        folder.open(Folder.READ_WRITE);
        Message[] messages = folder.getMessages();
        for (int i = 0; i < messages.length && result.size() < max; i++) {
            Message message = messages[i];
            if (deletes && (!message.getFlags().contains(Flag.DELETED))) {
                message.setFlag(Flag.DELETED, true);
            }
            result.add(message);
        }
    } finally {
        if (folder != null) {
            try {
                folder.close(true);
            } catch (Exception e) {
            }
        }
    }
}
你在我安 2024-12-09 20:03:25

下面列出了一些默认的 GMAIL 文件夹名称。

INBOX
Notes
Personal
Receipts
Work
[Gmail]/All Mail
[Gmail]/Drafts
[Gmail]/Important
[Gmail]/Sent Mail
[Gmail]/Spam
[Gmail]/Starred
[Gmail]/Trash

Code ex:
Folder mailFolder = store.getFolder("[Gmail]/Trash");

Below listed are some of the default GMAIL folder names.

INBOX
Notes
Personal
Receipts
Work
[Gmail]/All Mail
[Gmail]/Drafts
[Gmail]/Important
[Gmail]/Sent Mail
[Gmail]/Spam
[Gmail]/Starred
[Gmail]/Trash

Code ex:
Folder mailFolder = store.getFolder("[Gmail]/Trash");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文