我在AWS Lambda上使用javax.mail包时遇到问题。
当尝试获取电子邮件的内容时,在本地运行时,getContent()方法将返回MimeMultipart
对象。在挂载到AWS lambda java8 docker映像时,它返回IMAPInputStream
。
我已经看到了解决此问题的方法,包括在当前线程上设置类加载器,例如:Thread.currentThread().setContextClassLoader(this.class.getClassLoader());
但是,这对我不起作用。本地运行的类加载器与针对lambda java8 docker映像运行的类加载器不同。
本地:sun.misc.Launcher$AppClassLoader
码头工人:java.net.URLClassLoader
我尝试像这样使用系统类加载器sun.misc.Launcher$AppClassLoader
显式加载类:
Class c = java.lang.ClassLoader.getSystemClassLoader().loadClass("helpers.MailReader");
MailReader mail = (MailReader)c.newInstance();
这在本地运行时效果很好,但是再次对docker镜像失败:
java.lang.ClassNotFoundException: helpers.MailReader
因此,跨环境的系统类加载器有所不同。 我对Java不太了解,似乎我只是缺少一些简单的东西并使它变得比实际更复杂。 可能导致这种情况的环境有什么不同?
编辑:调试javax.activation.debug的输出:
MailcapCommandMap: load HOME
new MailcapFile: file /home/sbx_user1051/.mailcap
MailcapCommandMap: load SYS
new MailcapFile: file /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.141-1.b16.32.amzn1.x86_64/jre/lib/mailcap
MailcapCommandMap: load JAR
MailcapCommandMap: getResources
MailcapCommandMap: URL file:/var/task/META-INF/mailcap
new MailcapFile: InputStream
parse: text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain
Type: text/plain
Command: content-handler, Class: com.sun.mail.handlers.text_plain
parse: text/html;; x-java-content-handler=com.sun.mail.handlers.text_html
Type: text/html
Command: content-handler, Class: com.sun.mail.handlers.text_html
parse: text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml
Type: text/xml
Command: content-handler, Class: com.sun.mail.handlers.text_xml
parse: multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed; x-java-fallback-entry=true
Type: multipart/*
Command: content-handler, Class: com.sun.mail.handlers.multipart_mixed
parse: message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822
Type: message/rfc822
Command: content-handler, Class: com.sun.mail.handlers.message_rfc822
MailcapCommandMap: successfully loaded mailcap file from URL: file:/var/task/META-INF/mailcap
MailcapCommandMap: load DEF
new MailcapFile: InputStream
parse: image/gif;; x-java-view=com.sun.activation.viewers.ImageViewer
Type: image/gif
Command: view, Class: com.sun.activation.viewers.ImageViewer
parse: image/jpeg;; x-java-view=com.sun.activation.viewers.ImageViewer
Type: image/jpeg
Command: view, Class: com.sun.activation.viewers.ImageViewer
parse: text/*;; x-java-view=com.sun.activation.viewers.TextViewer
Type: text/*
Command: view, Class: com.sun.activation.viewers.TextViewer
parse: text/*;; x-java-edit=com.sun.activation.viewers.TextEditor
Type: text/*
Command: edit, Class: com.sun.activation.viewers.TextEditor
Merging commands for type text/*
MailcapCommandMap: successfully loaded mailcap file: /META-INF/mailcap.default
MailcapCommandMap: createDataContentHandler for multipart/alternative
search DB #1
search DB #2
search fallback DB #1
got content-handler
class com.sun.mail.handlers.multipart_mixed
Can't load DCH com.sun.mail.handlers.multipart_mixed; Exception: java.lang.ClassNotFoundException: com/sun/mail/handlers/multipart_mixed
search fallback DB #2
我在网上发现的与此相关的解决方案都没有帮助