我已经配置了 courier 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 mail 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()
}
}
}