我正在尝试使用Scala和JavaMail + gimaps(Gmail扩展程序)。我目前正在使用v1.5.1,scala 2.10.3,java JDK 1.7.0_45。事情几乎可行,我可以获得GmailSSLStore,我可以将Gmail文件加载到名为allMail的变量中。
但是,当我调用allMail.getMessages()时,而不是像我期望的那样返回GmailMessage,而是返回正常的IMAPMessages。知道为什么会这样吗?我介绍了代码,看起来可能没有正确使用GmailProtocol,但我不知道为什么会这样。任何帮助将不胜感激!
这是在Akka,BTW。
var store: GmailStore = _
var allMail: GmailFolder = _
override def preStart = {
val props = System.getProperties()
props.put("mail.debug", "true")
props.put("mail.store.protocol", "gimaps")
val session = Session.getDefaultInstance(props, null)
store = session.getStore("gimaps").asInstanceOf[GmailStore]
store.connect("test@gmail.com", "Password" )
val folders = store.getDefaultFolder.list("*").map(m => m.asInstanceOf[GmailFolder])
allMail = folders.filter(m => m.getAttributes.contains("\\All")).head
allMail.open(Folder.READ_ONLY)
}
//Some Code Elided
def getMail() = {
val ms = allMail.getMessages()
val fp = new FetchProfile()
fp.add(GmailFolder.FetchProfileItem.MSGID)
fp.add(GmailFolder.FetchProfileItem.THRID)
fp.add(GmailFolder.FetchProfileItem.LABELS)
allMail.fetch(ms, fp)
val messages = ms.map(m => {
m.asInstanceOf[GmailMessage] // <--- This line crashes, m is IMAPMessage
})
}
输出如下。一个注意事项,我只看到可能存在的问题,为什么在那里列出JavaMail 1.4.4?在Built.SBT
中非常清楚地设置了依赖关系 "com.sun.mail" % "javax.mail" % "1.5.1",
"com.sun.mail" % "gimap" % "1.5.1"
控制台输出:
DEBUG: JavaMail version 1.4.4
DEBUG: URL jar:file:/C:/Users/Patrick/.ivy2/cache/com.sun.mail/gimap/jars/gimap-1.4.7.jar!/META-INF/javamail.providers
DEBUG: successfully loaded resource: jar:file:/C:/Users/Patrick/.ivy2/cache/com.sun.mail/gimap/jars/gimap-1.4.7.jar!/META-INF/javamail.providers
DEBUG: URL jar:file:/C:/Source/GIT/Core/sync/lib/gimap-1.5.1.jar!/META-INF/javamail.providers
DEBUG: successfully loaded resource: jar:file:/C:/Source/GIT/Core/sync/lib/gimap-1.5.1.jar!/META-INF/javamail.providers
DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
DEBUG: Tables of loaded providers
DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], com.sun.mail.gimap.GmailSSLStore=javax.mail.Provider[STORE,gimaps,com.sun.mail.gimap.GmailSSLStore,Oracle], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], com.sun.mail.gimap.GmailStore=javax.mail.Provider[STORE,gimap,com.sun.mail.gimap.GmailStore,Oracle], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc]}
DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], gimap=javax.mail.Provider[STORE,gimap,com.sun.mail.gimap.GmailStore,Oracle], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc], gimaps=javax.mail.Provider[STORE,gimaps,com.sun.mail.gimap.GmailSSLStore,Oracle]}
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
DEBUG: getProvider() returning javax.mail.Provider[STORE,gimaps,com.sun.mail.gimap.GmailSSLStore,Oracle]
DEBUG: mail.imap.fetchsize: 16384
DEBUG: mail.imap.statuscachetimeout: 1000
DEBUG: mail.imap.appendbuffersize: -1
DEBUG: mail.imap.minidletime: 10
DEBUG: trying to connect to host "imap.gmail.com", port 993, isSSL true
* OK Gimap ready for requests from 173.164.155.206 zl9mb39448964pac
A0 CAPABILITY
* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 XYZZY SASL-IR AUTH=XOAUTH AUTH=XOAUTH2 AUTH=PLAIN AUTH=PLAIN-CLIENTTOKEN
A0 OK Thats all she wrote! zl9mb39448964pac
DEBUG IMAP: AUTH: XOAUTH
DEBUG IMAP: AUTH: XOAUTH2
DEBUG IMAP: AUTH: PLAIN
DEBUG IMAP: AUTH: PLAIN-CLIENTTOKEN
DEBUG: protocolConnect login, host=imap.gmail.com, user=test@gmail.com, password=<non-null>
A1 AUTHENTICATE PLAIN
+
AHRlc3RAc3luYXRhLmNvbQBTeW5hdGExIQ==
* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 UIDPLUS COMPRESS=DEFLATE ENABLE MOVE CONDSTORE ESEARCH
A1 OK test@gmail.com Test Account authenticated (Success)
A2 CAPABILITY
* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 UIDPLUS COMPRESS=DEFLATE ENABLE MOVE CONDSTORE ESEARCH
A2 OK Success
A3 LIST "" "*"
* LIST (\HasNoChildren) "/" "INBOX"
* LIST (\Noselect \HasChildren) "/" "[Gmail]"
* LIST (\HasNoChildren \All) "/" "[Gmail]/All Mail"
* LIST (\HasNoChildren \Drafts) "/" "[Gmail]/Drafts"
* LIST (\HasNoChildren \Important) "/" "[Gmail]/Important"
* LIST (\HasNoChildren \Sent) "/" "[Gmail]/Sent Mail"
* LIST (\HasNoChildren \Junk) "/" "[Gmail]/Spam"
* LIST (\HasNoChildren \Flagged) "/" "[Gmail]/Starred"
* LIST (\HasNoChildren \Trash) "/" "[Gmail]/Trash"
A3 OK Success
DEBUG: connection available -- size: 1
A4 EXAMINE "[Gmail]/All Mail"
* FLAGS (\Answered \Flagged \Draft \Deleted \Seen $Phishing $NotPhishing)
* OK [PERMANENTFLAGS ()] Flags permitted.
* OK [UIDVALIDITY 11] UIDs valid.
* 4 EXISTS
* 0 RECENT
* OK [UIDNEXT 590] Predicted next UID.
* OK [HIGHESTMODSEQ 148419]
A4 OK [READ-ONLY] [Gmail]/All Mail selected. (Success)
DEBUG IMAP: IMAPProtocol noop
A5 NOOP
A5 OK Success
A6 CLOSE
[ERROR] [02/21/2014 09:37:38.404] [ConnectionSystem-akka.actor.default-dispatcher-4] [akka://ConnectionSystem/user/MainConnectionManager/googleAppsConnectionManager-5305184d4500004500e8fe66/gmailManager/$a] com.sun.mail.imap.IMAPMessage cannot be cast to com.sun.mail.gimap.GmailMessage
java.lang.ClassCastException: com.sun.mail.imap.IMAPMessage cannot be cast to com.sun.mail.gimap.GmailMessage
at com.test.sync.google.GmailSyncer$$anonfun$firstSync$1$$anonfun$2.apply(GmailSyncer.scala:90)
at com.test.sync.google.GmailSyncer$$anonfun$firstSync$1$$anonfun$2.apply(GmailSyncer.scala:89)
at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:244)
at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:244)
at scala.collection.IndexedSeqOptimized$class.foreach(IndexedSeqOptimized.scala:33)
at scala.collection.mutable.ArrayOps$ofRef.foreach(ArrayOps.scala:108)
at scala.collection.TraversableLike$class.map(TraversableLike.scala:244)
at scala.collection.mutable.ArrayOps$ofRef.map(ArrayOps.scala:108)
at com.test.sync.google.GmailSyncer$$anonfun$firstSync$1.applyOrElse(GmailSyncer.scala:89)
at akka.actor.ActorCell.receiveMessage(ActorCell.scala:498)
at akka.actor.ActorCell.invoke(ActorCell.scala:456)
at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:237)
at akka.dispatch.Mailbox.run(Mailbox.scala:219)
at akka.dispatch.ForkJoinExecutorConfigurator$AkkaForkJoinTask.exec(AbstractDispatcher.scala:386)
at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260)
at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339)
at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979)
at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)
A6 OK Returned to authenticated state. (Success)
DEBUG: added an Authenticated connection -- size: 1
A7 LOGOUT
* BYE LOGOUT Requested
A7 OK 73 good day (Success)
DEBUG: IMAPStore connection dead
DEBUG: IMAPStore cleanup, force false
DEBUG: IMAPStore cleanup done
答案 0 :(得分:3)
我猜你在类路径中同时拥有JavaMail 1.4.4和JavaMail 1.5.1。尽管它正在从JavaMail 1.5.1中找到“gimaps”提供程序,但它正在使用1.4.4中的IMAP类,而这些类没有允许gimaps提供程序扩展它并提供Gmail特定支持的钩子。 / p>
答案 1 :(得分:1)
好的,我们终于真正想到了这一点,我希望这可以帮助别人。
基本上,Lift对Java Mail 1.4.4具有传递依赖性,但它与我们引用的包装不同(javax.mail,javamail,1.4.4 vs sun.com.mail,javax .mail,1.5.1)。因此,我几乎把它归类为一个bug,但是重叠的Jars包含重叠的包名,但是SBT并没有将它识别为真正的依赖冲突。根据JVM的情绪,可以加载1.4.4 jar或1.5.1 jar,这意味着当我们认为我们调用1.5.1库时,我们现在再次看到NoMethodFound异常,但是实际上正在加载1.4.4库。
我们通过搜索依赖项解析日志对此进行了调试,但是在我们可以使用常春藤依赖性报告查看哪些库依赖于旧的Java Mail组件后,我们也学到了这一点。