I have been using JavaMail quite for sometime to develop a simple Mail application. I have also developed a simple search facility using SearchTerm concept in JavaMail. I wanted to search emails by sender, recevier, date, content or subject. So, I have the following sample SearchTerm combinations for the above parameters:
SearchTerm searchSenderOrSubjectTerm = new OrTerm(termSender, termSub); SearchTerm searchSenderOrDate = new OrTerm(termSender, termRecvDate); SearchTerm searchSubjectOrSenderOrDate = new OrTerm(searchSenderOrSubjectTerm, searchSenderOrDate); SearchTerm searchSubjectOrContentOrSenderOrDate = new OrTerm(searchSubjectOrSenderOrDate, termContent); SearchTerm searchSubjectOrContentOrSenderOrRecvrOrDate = new OrTerm(searchSubjectOrContentOrSenderOrDate, termRecvr); //return the search results searchResults = folder.search(searchSubjectOrContentOrSenderOrRecvrOrDate); This is working fine and returns the required results. But the problem with this approach is that it is taking too much time to search and return the results. I was just wondering whether the problem is the internal SearchTerm implementation or from the above approach. So, can you guys share me your experience on this especially on the performance issue? This is taking too much time and I am not exactly sure where the problem is.
regards,
2 Answers
If you're using IMAP, the searching is all done on the server, so the performance depends on the server. If you're using POP3, the searching is done by downloading all the messages to the client and searching there. Use IMAP. :-)
You can simplify your search by using a single OrTerm with an array of all the other terms. I don't know if that will make any performance difference, however.
Unless you use Google's IMAP extensions, you are applying the search criteria locally.
To search on the server with JavaMail, you'll want to do something like this:
GmailStore store = (GmailStore) session.getStore("gimap"); store.connect("imap.gmail.com", "[", "[your-pw]"); GmailFolder inbox = (GmailFolder) store.getFolder("[Gmail]/All Mail"); inbox.open(Folder.READ_ONLY); Message[] foundMessages = inbox.search(new GmailRawSearchTerm("to:"));