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
Solved

SEARCH command using java mail API

Posted on 2002-04-10
5
488 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MySQL  on Tomcat 8 68
intellij error 2 17
Notify sent to other threads in Java 9 33
Java basic valueOf question 1 29
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This video teaches viewers about errors in exception handling.

861 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