IMAP空闲方法挂起+ javax.mail + groovy

时间:2013-01-18 00:53:57

标签: grails groovy imap javax.mail

我已经配置了快递IMAP服务器并测试了IDLE功能,它在使用telnet时工作正常

  machinexxx:~$ telnet hostname 143
    Trying x.x.x.x...
    Connected to hostname.
    Escape character is '^]'.
    * OK [CAPABILITY IMAP4rev1 UIDPLUS CHILDREN NAMESPACE THREAD=ORDEREDSUBJECT THREAD=REFERENCES SORT QUOTA IDLE ACL ACL2=UNION STARTTLS] Courier-IMAP ready. Copyright 1998-2008 Double Precision, Inc.  See COPYING for distribution information.
    a login loginname password
    a OK LOGIN Ok.
    b select INBOX
    * FLAGS (\Draft \Answered \Flagged \Deleted \Seen \Recent)
    * OK [PERMANENTFLAGS (\* \Draft \Answered \Flagged \Deleted \Seen)] Limited
    * 27 EXISTS
    * 0 RECENT
    * OK [UIDVALIDITY 1358238865] Ok
    * OK [MYRIGHTS "acdilrsw"] ACL
    b OK [READ-WRITE] Ok
    b IDLE
    + entering ENHANCED idle mode
    * 28 EXISTS
    * 1 RECENT

当我从我的grails应用程序中尝试相同时,它只是挂在java邮件IMAPFolder idle()命令上,并且没有得到任何更新。有什么想法吗?我试图搜索其他问题看起来没有直接的解决方案(他们建议多线程或创建一个线程来调用idle()方法,然后重新运行相同的线程)有任何一个实现它请分享代码。谢谢!

  def serviceMethod() {


        log.info("Email Service check for emails!")

        runAsync {   
        Properties props = new Properties()           


        props.setProperty "mail.store.protocol", "imap"
        props.setProperty "mail.imap.host", "hostname"  
        props.setProperty "mail.imap.port", "143"

        def sess = Session.getDefaultInstance props, null      
        def store = sess.getStore "imap"        

        IMAPFolder inbox

        try{


              store.connect("hostname", 143, "loginname", "password") 

              inbox = store.getFolder "INBOX" 

              inbox.open(Folder.READ_WRITE)

              inbox.idle(false)

              def messages = inbox.search(new FlagTerm(new Flags(Flags.Flag.SEEN), false))

              def contents, bodyPart
                messages.each { msg ->

                println("${msg.subject} ${msg.sender}")

              }        
        } 
        catch(MessagingException e){
            log.error "Issue with connecting to email store or accessing folder or messages: " + e
        }
        catch(Exception eX){
            log.error "enable to read email " + eX
        }
        finally {
          if(inbox){
              inbox.close(true)
             }
              store.close()
        }
    }
    }

0 个答案:

没有答案