Javamail get all emails form a specific sender

匿名 (未验证) 提交于 2019-12-03 09:52:54

问题:

In java I need to get all emails from a specific sender. There are couple of ways to do that as shown here: http://www.codejava.net/java-ee/javamail/using-javamail-for-searching-e-mail-messages

But I need to have the process to be done on gmail side. It is not fine for me to read all emails and then decide which one I should proceed.

Also in http://alvinalexander.com/java/javamail-multiple-search-terms-pop3-mailbox-yahoo you can search message body which it is great, but what about the sender (FROM part)? How can I filter it?

Also reading all emails each time is not good for our bandwidth (Of course we can mark the last read email and read just all new emails somehow like using UIDFolder)

Thanks

回答1:

I found it myself :)

Folder inbox = store.getFolder("INBOX"); inbox.open(Folder.READ_ONLY);  SearchTerm sender = new FromTerm(new InternetAddress("from@example.com")); Message[] messages = inbox.search(sender);  for (int i = 1; i < messages.length; i++) {     read_message(messages[i]); } 

All I needed was to add FromTerm!



回答2:

If you need to use server HW to search emails from specific sender you should use IMAP command. You need ImapClient class and method ExecuteSearch(). Search IMAP commad for you is

SEARCH UNSEEN FROM "somebody@example.org" 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!