The InboxEmailsReader is a new class introduced in the JavaEmailer library 0.2 to let Java application projects check email accounts inbox folders for incoming messages to automatically detect undelivered mails and update mailing lists to avoid wasting time sending emails that will never reach recipients during next mass mailing operations.
//Sampleprogrampackagepackagejavatestlab;//Forreadingtextfilesimportejel.*;importejel.EventManager.EventID;importjava.io.IOException;importjavax.mail.*;importjava.util.*;/****@authoremmr.rida*/publicclassMain{/***@paramargsthecommandlinearguments*/publicstaticvoidmain(String[]args)throwsNoSuchProviderException,MessagingException,IOException,InterruptedException{asyncSample();//Uncommenttousenonblockingservices//syncSample();//Uncommenttouseblockingservices}/***SamplecodeoftheInboxEmailsReaderthatusesblockingservicestoconnecttoanemailaccountandsearchforemailmessages*@throwsNoSuchProviderException*@throwsMessagingException*@throwsIOException*/privatestaticvoidsyncSample()throwsNoSuchProviderException,MessagingException,IOException{//EmailaccountlogininformationEmailAccountaccount=newEmailAccount("user@host.com","password","server_host_name",0,true,EmailAccount.ServerProtocol.IMAP);//Constructsanobjectthatwilllookforemailswhosesubjectcontains[Re:]InboxEmailsReaderreader=newInboxEmailsReader(account,"Re :",false,newFlags());System.out.println("Connecting to server...");if(reader.connect()){//ConnecttoserverSystem.out.println("Connected to the server.");Iteratorit=reader.iterator();//Gettheiteratorwhile(it.hasNext()){//WhiletherearemoreMessageobjectsMessagemsg=(Message)it.next();//RetrievetheMessageemailobjectSystem.out.println(msg.getFrom()[0].toString()+" | "+msg.getSubject()+" | "+msg.getSentDate().toString());}reader.close();//Cosethereader}else{//Errorif(reader.getException()!=null)reader.getException().printStackTrace();}System.out.println("Program terminated.");}/***SamplecodeoftheInboxEmailsReaderthatusesnonblockingservicestoconnecttoanemailaccountandsearchforemailmessages*@throwsNoSuchProviderException*@throwsMessagingException*@throwsIOException*@throwsInterruptedException*/privatestaticvoidasyncSample()throwsNoSuchProviderException,MessagingException,IOException,InterruptedException{//TheemailaccountlogininformationEmailAccountaccount=newEmailAccount("user@host","password","host_name",0,true,EmailAccount.ServerProtocol.POP3);//ConstructsanInboxEmailsReaderandsearchforemailswhosesubjectsmatchestheregularexpressionsInboxEmailsReaderreader=newInboxEmailsReader(account,"Re[\\s]{0,1}:.*",true,newFlags());//ConstructsalistenerobjectMyEventListenerlistener=newMyEventListener();//Registerthelistenerfortherighteventreader.registerEventListener(EventID.EID_NEXT_INBOX_EMAIL,listener);System.out.print("Connecting to server");reader.asyncConnect();//Trytoconnecttoserverwhile(reader.isNowConnecting()){//WaitfortheconnectionSystem.out.print('.');Thread.sleep(250);}System.out.println('.');if(reader.isConnected()){//ChecksuccessSystem.out.println("Connected to server.");}else{reader.getException().printStackTrace();return;}//Afterthecallathreadwillbecreatedandthesearchprocessbegins.//Foreveryemailmessagethatmatchesthesearchquery,aneventis//raisedsoastheregisteredlistenerswillchecktheemailmessage.reader.asyncIterator();while(reader.isIterating())Thread.sleep(250);//Waitforthesearchprocesstofinishreader.close();//ClosethereaderSystem.out.println("Program terminated.");}}/***Here's our simple listener for the InboxEmailsReader events*mustimplementtheEventListener*@authoremmr.rida*/classMyEventListenerimplementsejel.EventListener{/***TheasynchronoussearchprocesswillraisetheEventID.EID_NEXT_INBOX_EMAIL*eventforeachemaimessagefoundandcallthatfunctiontoprocesstheemailmessage*@paramsenderTheInboxEmailsReaderobjectwhoraisedtheevent*@paramevtIdTheeventID*@paramevtParamsParamsfortheevent*/publicvoideventListener(Objectsender,EventIDevtId,EventArgsevtParams){if(evtId==EventID.EID_NEXT_INBOX_EMAIL){NextInboxEmailEventArgsea=(NextInboxEmailEventArgs)evtParams;try{System.out.println(ea.getMessage().getFrom()[0].toString()+" | "+ea.getMessage().getSubject()+" | "+ea.getMessage().getSentDate().toString());//ea.setAbortStatus();//Toaborttheasynchronoussearchprocess}catch(Exceptionex){System.out.println(ex.getMessage());}}elseassertfalse:"That must never happens!";}}
Best regards :)
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
The InboxEmailsReader is a new class introduced in the JavaEmailer library 0.2 to let Java application projects check email accounts inbox folders for incoming messages to automatically detect undelivered mails and update mailing lists to avoid wasting time sending emails that will never reach recipients during next mass mailing operations.
Best regards :)