?
Solved

SEARCH command using java mail API

Posted on 2002-04-10
5
Medium Priority
?
505 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 150 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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 …
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:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month15 days, 2 hours left to enroll

770 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