jQuery in Action, 3rd edition
The moose likes Other JSE/JEE APIs and the fly likes POP3 JavaMail Client-How to Read Unread Mails only Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "POP3 JavaMail Client-How to Read Unread Mails only" Watch "POP3 JavaMail Client-How to Read Unread Mails only" New topic

POP3 JavaMail Client-How to Read Unread Mails only

Viswanatha GB

Joined: Sep 19, 2001
Posts: 16
Hi All,
I' have written the Java POP3 Mail Client.to get my new mails from my Yahoo account. but i'm unable to get only unreadmails. psl help me how to make my Mail Client to get only unread mails..snice now i'm getting all mails when i run this programm.
Programm is like this
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import java.io.*;
* A simple email receiver class.
public class SimpleReceiver
* MAIN method to receive messages from the mail server specified
* as command line arguments.
public static void main(String args[])
String popUser="myyahooID";//args[1];
String popPassword="xxxxxxx";//args[2];
//String popServer="pop.mail.yahoo.com";//args[0];
// String
receive(popServer, popUser, popPassword);
catch (Exception ex)
System.out.println("Usage: java com.lotontech.mail.SimpleReceiver"
+" popServer popUser popPassword");
* "receive" method to fetch messages and process them.
public static void receive(String popServer, String popUser
, String popPassword)
Store store=null;
Folder folder=null;
// -- Get hold of the default session --
Properties props = System.getProperties();
Session session = Session.getDefaultInstance(props, null);
// -- Get hold of a POP3 message store, and connect to it --
store = session.getStore("pop3");
store.connect(popServer, popUser, popPassword);
// -- Try to get hold of the default folder --
folder = store.getDefaultFolder();
if (folder == null) throw new Exception("No default folder");
// -- ...and its INBOX --
folder = folder.getFolder("INBOX");
if (folder == null) throw new Exception("No POP3 INBOX");
// -- Open the folder for read only --
// -- Get the message wrappers and process them --
Message[] msgs = folder.getMessages();
for (int msgNum = 0; msgNum < msgs.length; msgNum++)
// speakMessage(msgs[msgNum]);
catch (Exception ex)
// -- Close down nicely --
if (folder!=null) folder.close(false);
if (store!=null) store.close();
catch (Exception ex2) {ex2.printStackTrace();}
* "printMessage" method to print a message.
public static void printMessage(Message message)
// -- Get the header information --
String from=((InternetAddress)message.getFrom()[0]).getPersonal();
if (from==null) from=((InternetAddress)message.getFrom()[0]).getAddress();
System.out.println("FROM: "+from);
String subject=message.getSubject();
System.out.println("SUBJECT: "+subject);
// -- Get the message part (i.e. the message itself) --
Part messagePart=message;
Object content=messagePart.getContent();
// -- or its first body part if it is a multipart message --
if (content instanceof Multipart)
System.out.println("[ Multipart Message ]");
// -- Get the content type --
String contentType=messagePart.getContentType();
// -- If the content is plain text, we can print it --
if (contentType.startsWith("text/plain")
|| contentType.startsWith("text/html"))
InputStream is = messagePart.getInputStream();
BufferedReader reader
=new BufferedReader(new InputStreamReader(is));
String thisLine=reader.readLine();
while (thisLine!=null)
catch (Exception ex)

[ April 04, 2002: Message edited by: viswagb ]
[ July 02, 2002: Message edited by: Viswanatha GB ]

==============(**)=================<br />Viswanatha.G.B<br />Sr.Software Engineer<br />IBM<br />==============(**)=================
Lewin Chan
Ranch Hand

Joined: Oct 10, 2001
Posts: 214
Quoting from Sun's documentation about their bundled POP3 provider (which I'm assuming you are using) - the documentation is located in <installdir>/docs/sundocs
--Begin Quote--
POP3 supports no permanent flags (see Folder.getPermanentFlags()). In particular, the Flags.Flag.RECENT flag will never be set for POP3 messages. It's up to the application to determine which messages in a POP3 mailbox are "new". There are several strategies to accomplish this, depending on the needs of the application and the environment:
A simple approach would be to keep track of the newest message seen by the application.
An alternative would be to keep track of the UIDs (see below) of all messages that have been seen.
Another approach is to download all messages into a local mailbox, so that all messages in the POP3 mailbox are, by definition, new.
All approaches will require some permanent storage associated with the client.
--End Quote--
I think that pretty much answers your question

I have no java certifications. This makes me a bad programmer. Ignore my post.
Kareem Gad
Ranch Hand

Joined: Aug 06, 2001
Posts: 89
I was wondering if you can provide us with the source to the printMessage method referred to in your code.
I am new to java mail and found ur code interesting.

<b><i>KaReEm</i><br /><ul type="square"><li>SCJP-Free Range Web Developer <br /></ul></b>
Rob Ross

Joined: Jan 07, 2002
Posts: 2205
Hi viswagb.
Please change your name to comply with the naming policy to which you agreed when you registered here..

For your publicly displayed name,
use a first name, a space, and a last name.

You can change your name here:

You can also find the naming policy here:
Thank You!

SCJP 1.4
I agree. Here's the link: http://aspose.com/file-tools
subject: POP3 JavaMail Client-How to Read Unread Mails only
It's not a secret anymore!