javax.activation.UnsupportedDataTypeException: no object DCH for MIME type multipart/mixed;边界

2022-09-01 12:29:41

目前,我正在编写一个将侦听目录的代码。当目录使用.apk文件更新时,我会将此.apk文件发送到gmail帐户。我在我的程序中使用Jnotify和JAVA Mail。

我得到的错误是,

javax.mail.MessagingException: IOException while sending message;
  nested exception is:
javax.activation.UnsupportedDataTypeException: no object DCH for MIME type multipart/mixed; boundary="----=_Part_0_145238.1392728439484"

我寻找堆栈溢出中给出的解决方案以寻求帮助,但没有一个有帮助。

提前致谢

public void fileCreated(int wd, String rootPath, String name) {
    print("created " + rootPath + " : " + name);

    if (name.contains(".apk"))
      SendEmail(name);
    else
        System.out.println("Not the APK file");
}

void SendEmail(String strname){
    String Path = "D:/POC/Email/apk folder/"+strname;
    System.out.println("Path->" + Path);

    Properties props = new Properties();
    props.put("mail.smtp.host","173.194.78.108");
    props.put("mail.smtp.socketFactory.port", "465");
    props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.auth","true");
    props.put("mail.smtp.port","465");

    System.out.println("Properties has been set properly");

    Session session = Session.getDefaultInstance(props,
        new javax.mail.Authenticator(){
            protected PasswordAuthentication getPasswordAuthentication(){
                return new PasswordAuthentication("SenderEmailID@gmail.com", "senderPassword");
            }
        }
    );

    System.out.println("Session Created successfully");

    try{
        Message message = new MimeMessage(session); 
        message.setFrom(new InternetAddress("SenderEmailID@gmail.com"));
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("ToReceiverEmailID@gmail.com"));
        message.setSubject("Android : " + strname);

        MimeBodyPart msgbody = new MimeBodyPart();
        msgbody.setText("This is the message content which is sent using JAVA MAIL 1.4.5");
        Multipart mulpart = new MimeMultipart();
        mulpart.addBodyPart(msgbody);

        //Attachement Starts here.
        msgbody = new MimeBodyPart();
        javax.activation.DataSource source = new FileDataSource(Path);
        msgbody.setDataHandler(new DataHandler(source));
        msgbody.setFileName(strname);
        message.setContent(mulpart);

        System.out.println("Attached the message going to start transporting the mail");

        //If I've the code before sending the email is getting sent but without attachment. 
        //Thread.currentThread().setContextClassLoader( getClass().getClassLoader() );

        Transport.send(message);
        System.out.println("Mail Sent successfully");
    }
    catch(MessagingException msg){
        msg.printStackTrace();
    }
    catch(Exception e){
        e.printStackTrace();
    }
}

答案 1

JavaMail 依赖于一些配置文件将 MIME 类型映射到 Java 类(例如,映射到 )。这些配置文件是使用应用程序的类加载器加载的。如果 ClassLoader 不能正常工作,则不会找到这些配置文件。multipart/mixedjavax.mail.internet.MimeMultipart

您可以简单地在下面添加行..解决了问题。

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

答案 2

此问题可以通过执行以下两个步骤来获得解决方案。

  1. 确保 Java 邮件为 1.4.7。(以前我使用1.4.5,这导致了所有的混乱)。从 http://www.oracle.com/technetwork/java/index-138643.html 下载
  2. 在发送消息之前添加以下代码:
    Thread.currentThread().setContextClassLoader( getClass().getClassLoader() );

推荐