Solved

SEARCH command using java mail API

Posted on 2002-04-10
5
481 Views
Last Modified: 2013-11-24
hi,

The following IMAP Command list the message numbers of unseen mails :(syntax)

<machinename> search unseen
3 8 9

I want to do the same using java mail api.
I don't know how to use the search method in IMAPProtocol to get the above result.

help me.
Regards,
uma
0
Comment
Question by:umanarayan
  • 3
  • 2
5 Comments
 
LVL 9

Expert Comment

by:Venci75
ID: 6933516
try this:
 Properties props = new Properties();
 Session session = Session.getDefaultInstance(props,null);
 store = session.getStore("imap");

 store.connect("mail","user","pwd");
 folder = store.getFolder("INBOX");
 folder.open(Folder.READ_ONLY);
 Message message[] = folder.getMessages();
 for(int i =0, n =message.length; i<n; i++){
    if (!message[i].getFlags().contains(Flags.Flag.SEEN)) {
        System.out.println(i + ": " + message[i].getFrom()[0] + "\t" + message[i].getSubject());
    }
  }
 folder.close(false);
 store.close();
0
 

Author Comment

by:umanarayan
ID: 6933584
hi,

I accept that above code will work perfectly. But it will loop thru' all messages in the folder. Instead i like to fetch only the messages which has a particular flag(SEEN or RECENT etc).

In sense like to execute the imap search commands (RFC 2060) using the java mail API and fetch the results.

In java mail API, IMAPProtocol class contains the method search but it does not provides this functionality of fetching only message numbers.

Regards,
uma
0
 
LVL 9

Expert Comment

by:Venci75
ID: 6933608
ok - then try this:

 Properties props = new Properties();
 Session session = Session.getDefaultInstance(props,null);
 store = session.getStore("imap");

 store.connect("host","user","password");
 folder = store.getFolder("INBOX");
 folder.open(Folder.READ_ONLY);
 Flags flags = new Flags(Flags.Flag.SEEN);
 javax.mail.search.FlagTerm term = new javax.mail.search.FlagTerm(flags, false);
 Message message[] = folder.search(term);
 for(int i =0, n =message.length; i<n; i++){
    System.out.println(i + ": " + message[i].getFrom()[0] + "\t" + message[i].getSubject());
  }
 folder.close(false);
 store.close();
0
 

Author Comment

by:umanarayan
ID: 6933653
hi,

I need to fetch only UNSEEN mails.

thanks & regards,
uma
0
 
LVL 9

Accepted Solution

by:
Venci75 earned 50 total points
ID: 6933659
this is exactly what you want:
Flags flags = new Flags(Flags.Flag.SEEN);
javax.mail.search.FlagTerm term = new javax.mail.search.FlagTerm(flags, false);
Message message[] = folder.search(term);

if you use:
javax.mail.search.FlagTerm term = new javax.mail.search.FlagTerm(flags, true);

this will fetch the SEEN mails
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now