javamail - polling mailbox after every 10 minutes through java -


i have below program act mail box listener is configured pop3 protocol , when program executed connected mail box , download mails , attachments in specified folder , query how can modify below program in such way should keep polling mail box listner lets in every 10 minutes , download mails , polling should happen in every 10 minutes please advise how can modify below program achieve this..

import java.io.*; import java.util.*; import javax.mail.*;  import javax.mail.internet.mimebodypart;  public class receivemailpop3 {   private static final string host = "pop.gmail.com";   private static final string username = "myemail@gmail.com";   private static final string password = "******";    public static void doit() throws messagingexception, ioexception {     folder folder = null;     store store = null;     try {       properties props = new properties();       props.put("mail.store.protocol", "pop3s"); // google uses pop3s not pop3       session session = session.getdefaultinstance(props);       // session.setdebug(true);       store = session.getstore();       store.connect(host, username, password);       folder = store.getdefaultfolder().getfolder("inbox");       folder.open(folder.read_only);       message[] messages = folder.getmessages();       system.out.println("no of messages : " + folder.getmessagecount());       system.out.println("no of unread messages : " + folder.getunreadmessagecount());       (int i=0; < messages.length; ++i) {         system.out.println("message #" + (i + 1) + ":");         message msg = messages[i];         string = "unknown";         if (msg.getreplyto().length >= 1) {           = msg.getreplyto()[0].tostring();         }         else if (msg.getfrom().length >= 1) {           = msg.getfrom()[0].tostring();         }         string subject = msg.getsubject();         system.out.println("saving ... " + subject +" " + from);         // may want replace spaces "_"         // files saved temp directory         string filename = "c:/temp/" +  subject;         saveparts(msg.getcontent(), filename);       }     }     {       if (folder != null) { folder.close(true); }       if (store != null) { store.close(); }     }   }    public static void saveparts(object content, string filename)   throws ioexception, messagingexception   {     outputstream out = null;     inputstream in = null;     try {       if (content instanceof multipart) {         multipart multi = ((multipart)content);         int parts = multi.getcount();         (int j=0; j < parts; ++j) {           mimebodypart part = (mimebodypart)multi.getbodypart(j);           if (part.getcontent() instanceof multipart) {             // part-within-a-part, recursion...             saveparts(part.getcontent(), filename);           }           else {             string extension = "";             if (part.ismimetype("text/html")) {               extension = "html";             }             else {               if (part.ismimetype("text/plain")) {                 extension = "txt";               }               else {                 //  try name of attachment                 extension = part.getdatahandler().getname();               }               filename = filename + "." + extension;               system.out.println("... " + filename);               out = new fileoutputstream(new file(filename));               in = part.getinputstream();               int k;               while ((k = in.read()) != -1) {                 out.write(k);               }             }           }         }       }     }     {       if (in != null) { in.close(); }       if (out != null) { out.flush(); out.close(); }     }   }    public static void main(string args[]) throws exception {     receivemailpop3.doit();   } }  

you can use timer. using timer schedule task @ regular intervals.

  timer timer = new timer();   long interval = (10*60*1000) ; // 10 minutes    timer.schedule( new timertask() {        public void run() {           receivemailpop3.doit();        }   }, 0, interval);  

if want stop scheduling, can use timer.cancel();

one more point worth noting : if receivemailpop3.doit(); start @ 12:00 , takes 9 minutes execute, next execution of receivemailpop3.doit(); @ 12:19 because timer wait interval amount of time between each execution. if want execute @ at 10 minutes without caring previous execution, have use timer.scheduleatfixedrate


Comments

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -