File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Other JSE/JEE APIs and the fly likes Java Mail pop3 listener Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "Java Mail pop3 listener" Watch "Java Mail pop3 listener" New topic
Author

Java Mail pop3 listener

Eswar Varanasi
Greenhorn

Joined: Jun 03, 2007
Posts: 20
How to be notified when a new mail arrives if pop3 server (javax.mail) is used.

I used the below code to connect to gmail pop3 server and now i want to check or be notified
for any new mails. Is there any listener concept to be implemented for this.

any help is greatly appreciated.


( the below code snipped is used to login & check for arrived mails)



String TEMP="C:\\TEMP"
String host = "mail.gmail.com";
String username = "GMAIL_USERNAME@gmail.com";
String password = "PASSWORD";
String subject = "BetaFile";
String srcDirectory = "";
String destDirectory = "";
String fileName = null;
Message message[] = null;
Folder folder = null;
Properties props = System.getProperties();
props.put("mail.transport.protocol","pop");
props.setProperty("mail.pop3.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.pop3.host", " pop.gmail.com");
props.put("mail.pop3.port", "995");
props.put("mail.pop3.starttls.enable","true");
props.setProperty("mail.pop3.socketFactory.fallback", "false");

Session session = Session.getInstance(props, null);

URLName urlName = new URLName("pop3","pop.gmail.com",995,null,username, password);

Store store = session.getStore(urlName);
store.connect();
MailStoreListener mailStoreListener = new MailStoreListener ();
store.addStoreListener(mailStoreListener);
while(true) {
folder = store.getFolder("INBOX");
folder.open(Folder.READ_ONLY);

message = folder.getMessages();

for( int i=0; i < 40 ; i++ ) System.out.print(" - ");
System.out.println("\n Message Count : "+message.length);
for( int i=0; i < 40 ; i++ ) System.out.print(" - ");

for (int i=0, n=message.length; i<n; i++) {
Message mess = message[i];
System.out.println("\n"+i + ": " + mess.getFrom()[0] + "\t" + mess.getSubject());
}
for( int i=0; i < 40 ; i++ ) System.out.print(" - ");
System.out.println("\n");
for ( int i = 0, n = message.length ; i < n; i++ ) {

Message mess = message[i];
try {
if( mess.isMimeType("multipart/*") ) {
if( mess.getSubject().equalsIgnoreCase( subject ) ) {

Multipart multipart = ( Multipart ) mess.getContent();
int cc;

for(int j = 0 , m = multipart.getCount() ; j < m ; j++ ) {

Part part = multipart.getBodyPart( j );
String disposition = part.getDisposition();

if( disposition != null && ( disposition.equals(Part.ATTACHMENT ) || ( disposition.equals( Part.INLINE ) ) ) ) {

System.out.println(" File : "+ part.getFileName() + " : written to the temp Location : "+ TEMP + " .");
FileOutputStream outfile = new FileOutputStream ( TEMP + part.getFileName() );
BufferedInputStream bis = new BufferedInputStream ( part.getInputStream() );
fileName = part.getFileName() ;

while( ( cc = bis.read() ) != -1 ) {
outfile.write( cc );
}
outfile.close();
}
}
}
}
} catch ( FileNotFoundException ffe ){
//mess.setFlag( Flags.Flag.RECENT , true );
System.out.println("Exception : Error accessing/writing file to FTP Location ( Check the path or file write privileges to ftp location ) ");
}
catch ( Exception ex ) {
System.out.println("Exception In Java Mail: " + ex.getMessage() );
}
}

}
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42267
    
  64
Welcome to JavaRanch.

You could run that code as a cron job (or Service, depending on your OS), though probably without the parts that download the actual mail. Then the program can do something if new mail arrives (play a sound, ...).


Ping & DNS - my free Android networking tools app
Eswar Varanasi
Greenhorn

Joined: Jun 03, 2007
Posts: 20
Hi,
what i am exactly look is, if there is any listener kind of thing to get notified if any new mail arrives in java mail api?.

Any assistance is greatly appreciated.

thanks & regards,
Ravi
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42267
    
  64
No, there isn't. But you can create one yourself in the way I suggested.
Eswar Varanasi
Greenhorn

Joined: Jun 03, 2007
Posts: 20
Hi Dittmer,
I tried using Quartz scheduler, making the process as a cron job. But, as i told you, i am looking for listner kind of thing.

Also, i looked into the Folder Object MessageCountListener which invokes MessageAdded method when the count of the messages get changed ( incr/ decr). But how do you think we can make use of it.

any suggestion is greatly appreciated.

thanks & regards,
Eswar.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42267
    
  64
Quartz or cron do similar things; you only need one or the other, not both. I'd use cron for this, so that there's no reliance on a Java container in which Quartz would run.

As I said above, no such listener exists - you'll need to implement it yourself.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java Mail pop3 listener