简体   繁体   中英

Gmail fetching mails from Sent items as well

I have the following code to connect to an inbox of a mail server:

Store popStore = popSession.getStore("pop3");
popStore.connect(address, userName, password);
Folder  inboxFolder = popStore.getFolder("Inbox");

Post this i check for new mails. Now when I connect to Gmail, I am getting mails from Sent Items as well when actually it is supposed to be only from the Inbox folder. With Yahoo this is working fine.

Any Idea what can be causing this issue in Gmail?

Edit: I have tried with INBOX as well and the result is the same

Following is a code snippet. When I checked with gmail, there is no overlap between inbox and sent mail. (This should have been a comment, posting as answer for formatting)

javax.mail.Folder[] folders = store.getDefaultFolder().list("*");
for (javax.mail.Folder folder : folders) {
    if ((folder.getType() & javax.mail.Folder.HOLDS_MESSAGES) != 0) {
        if (folder.getFullName().equalsIgnoreCase("[Gmail]/Sent Mail") 
                || folder.getFullName().equalsIgnoreCase("Inbox")) {
            System.out.println(folder.getFullName() + ": " + folder.getMessageCount());
            folder.open(Folder.READ_ONLY);
            for (Message m : folder.getMessages(
                               folder.getMessageCount() - 5, 
                               folder.getMessageCount())) {
                System.out.println("m.getSubject() = " + m.getSubject());
            }
            folder.close(true);
        }
    }
}

firstly try this

Folder folder = store.getDefaultFolder();
folder = folder.getFolder("INBOX");
folder.open(Folder.READ_WRITE);

Interesting issue. I did a little research and found this post in which Google says:

When you enable POP, all messages are downloaded to your client, except for Spam, Trash, and Chats. If you don't want messages that you send from the web interface downloaded to your mail client's inbox, we suggest creating a filter within your client.

To create a filter by sender, you can do this:

String filter = "Not([SenderEmailAddress] = 'XXXXX@gmail.com')";
Items inboxItems = inboxFolder.Items.Restrict(filter);

where XXXXX@gmail.com is your email address. This filter will give you only the items which are sent by someone other than yourself. Additionally, the Restrict method can be replaced with Find , but Restrict will be much faster for larger datasets.

when you communicate through mail using reply or reply to all in gmail it will considered as inbox mail. Because it is conversation view. so that your sent mail is also an inbox mail. so you will get that mails in your messages.

Read this official google answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM