Searching Mails

In this post under Java Mail, I will explain with example how to search mails in an inbox.

To start a search we need to establish criterias and only those mails matching the criterias will be displayed.

To establish criteria we will take the help of javax.mail.search.SearchTerm abstract class.

Even though SearchTerm is an abstract class, Java API provides different implementations of it to come up with specialized criterias.

There is a hierarchy of classes which implement SearchTerm class directly or indirectly. We can take help of those concrete class to create our criterias.

In this post I will show an example, which will search inbox for a mail received from particular email address.

Below is the complete code

Main Code


1  package package7;
2  
3  import java.util.Properties;
4  
5  import javax.mail.Address;
6  import javax.mail.Authenticator;
7  import javax.mail.Folder;
8  import javax.mail.Message;
9  import javax.mail.MessagingException;
10 import javax.mail.NoSuchProviderException;
11 import javax.mail.Session;
12 import javax.mail.Store;
13 import javax.mail.internet.InternetAddress;
14 import javax.mail.search.FromTerm;
15 import javax.mail.search.SearchTerm;
16 
17 public class Example7 {
18     public static void main(String[] args) throws NoSuchProviderException, MessagingException {
19         Properties properties = new Properties();
20         Authenticator authenticator = null;
21         Session session = Session.getInstance(properties, authenticator);
22         Store store = session.getStore("pop3s");
23         store.connect("pop.gmail.com", 995, "********************@gmail.com", "***********");
24         Folder inbox = store.getFolder("INBOX");
25         inbox.open(Folder.READ_ONLY);
26         
27         Address fromAddress = new InternetAddress("*******************");
28         SearchTerm searchTerm = new FromTerm(fromAddress);
29         
30         Message[] messages = inbox.search(searchTerm);
31         
32         for(int i = 0; i < messages.length; i++) {
33             Message message = messages[i];
34             System.out.println(message.getSubject() + ":" + message.getMessageNumber());
35         }
36         inbox.close(false);
37         store.close();
38     }
39 }

At line 25, we open the inbox for read only.

At line 27, we create an instance of InternetAddress named fromAddress, which represents the email address for which, we will be searching the inbox for mails coming from.

At line 28, we create an instance of FromTerm named as searchTerm and passing the fromAddress as an argument.

At line 30, we search the inbox by calling “search” method on the inbox and passing the searchTerm as an argument.

The result of “search” method is an array of messages.

From line 32 to 34, we loop through the array of messages coming from fromAddress and displaying the subject and message number to the console.

At line 36, we close the inbox

At line 37, we close the store.

This is how we have to search inbox for mails matching our criterias.

Leave a Reply