Solved

SEARCH command using java mail API

Posted on 2002-04-10
5
480 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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

746 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

12 Experts available now in Live!

Get 1:1 Help Now