Android 发送邮件时遭遇 : no object DCH for MIME type multipart/mixed

I create a message:

msg = new MimeMessage(session);
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.attachFile(f1);
MimeBodyPart mbp2 = new MimeBodyPart();
mbp2.attachFile(f2);
MimeBodyPart mbp3 = new MimeBodyPart();
mbp3.attachFile(f3);
Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp1);
mp.addBodyPart(mbp2);
mp.addBodyPart(mbp3);
msg.setContent(mp);
...

then send it:

SMTPTransport transport = (SMTPTransport)session.getTransport();
transport.connect();
transport.sendMessage(msg, new Address[] {new InternetAddress(...)});

and get MessagingException: IOException while sending message

UnsupportedDataTypeException: no object DCH for MIME type multipart/mixed:



 There is something wrong with MailCap, javamail can not find a handler for such multipart/mixed part. 

 I solved this issue by adding this (before mail creation and delivery)

             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);

 Note that JAF comes bundled in Java >= 6

 

posted @ 2014-11-11 22:14  MMLoveMeMM  阅读(1396)  评论(0编辑  收藏  举报