JavaMail在Android上使用

时间:2021-04-10 18:09:41

           下载http://code.google.com/p/javamail-android/  虽然n年没更新了,但还是将就用吧,遇到问题自己解决。

            说说纠结了2两天的问题:IMAPInputStream cannot be cast to javax.mail.Multipart,这种问题真是折腾人。

            我建一个普通的java项目,导入javamail 1.4.5的包能用,我直接把javamail 1.4.5的源码放入项目中就不能用了,仍然报的是"IMAPInputStream cannot be cast to javax.mail.Multipart"。纠结至于认为这bug肯定是什么地方打包或配置文件的问题——诡异。源码看得头痛,继续google,然后跑去oracle官网上看,为嘛去oracle官网,因为java现在归属在oracle。发现答案在这里:https://forums.oracle.com/thread/2328407?tstart=105,大牛还是大牛,非吾辈码农能及。

         

// There is something wrong with MailCap, javamail can not find a handler for the multipart/mixed part, so this bit needs to be added. 
MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");
CommandMap.setDefaultCommandMap(mc);
}