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

标签 java email jakarta-mail ioexception jnotify

目前我正在编写一个将监听目录的代码。当使用 .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"

我在 stackoverflow 中寻找解决方案以寻求帮助,但没有一个有用。

提前致谢

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

最佳答案

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

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

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

关于javax.activation.UnsupportedDataTypeException : no object DCH for MIME type multipart/mixed; boundary,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21856211/

相关文章:

java - 实体没有表单对象所需的字段

java - 尝试从 Java 中的控制台读取

php - 使用 codeigniter 中的电子邮件库发送垃圾邮件

java - 将 com.sun.mail.imap.IMAPMessage 转换为字符串

java - 如何使用java邮件在单个邮件中发送由函数返回的两个附件?

Java邮件 : How to use different SOCKS5 for different threads?

java - JSP + Eclipse + TomCat : cannot be resolved to a type

php - 通过 PHP 代码访问我的 Gmail 收件箱

php - 使用 aws 在 laravel 5.2 电子邮件中自定义字体

java - 如何在java数组中实现线性插值方法?