This week's book giveaways are in the Jython/Python and Object-Oriented programming forums.
We're giving away four copies each of Machine Learning for Business: Using Amazon SageMaker and Jupyter and Object Design Style Guide and have the authors on-line!
See this thread and this one for details.
Win a copy of Machine Learning for Business: Using Amazon SageMaker and JupyterE this week in the Jython/Python forum
or Object Design Style Guide in the Object-Oriented programming forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

Java Mail pop3 listener

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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() );
}
}

}
 
Rancher
Posts: 43011
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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, ...).
 
Eswar Varanasi
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43011
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, there isn't. But you can create one yourself in the way I suggested.
 
Eswar Varanasi
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43011
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
You learn how to close your eyes and tell yourself "this just isn't really happening to me." Tiny ad:
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!