• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

POP3 JavaMail Client-How to Read Unread Mails only

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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[])
{
try
{
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");
}
System.exit(0);
}
/**
* "receive" method to fetch messages and process them.
*/
public static void receive(String popServer, String popUser
, String popPassword)
{
Store store=null;
Folder folder=null;
try
{
// -- 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 --
folder.open(Folder.READ_ONLY);
// -- Get the message wrappers and process them --
Message[] msgs = folder.getMessages();
for (int msgNum = 0; msgNum < msgs.length; msgNum++)
{
printMessage(msgs[msgNum]);
// speakMessage(msgs[msgNum]);
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
finally
{
// -- Close down nicely --
try
{
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)
{
try
{
// -- 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)
{
messagePart=((Multipart)content).getBodyPart(0);
System.out.println("[ Multipart Message ]");
}
// -- Get the content type --
String contentType=messagePart.getContentType();
// -- If the content is plain text, we can print it --
System.out.println("CONTENT:"+contentType);
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)
{
System.out.println(thisLine);
thisLine=reader.readLine();
}
}
System.out.println("-----------------------------");
}
catch (Exception ex)
{
ex.printStackTrace();
}
}

/*************************************/
/**************************************/
Viswa
[ April 04, 2002: Message edited by: viswagb ]
[ July 02, 2002: Message edited by: Viswanatha GB ]
 
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
viswagb,
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.
Thanks.
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
here

You can also find the naming policy here:
http://www.javaranch.com/name.jsp
Thank You!
 
and POOF! You're gone! But look, this tiny ad is still here:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic