javax.activation.UnsupportedDataTypeException: MIME 类型 multipart/mixed 没有对象 DCH;边界

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21856211/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 11:05:02  来源:igfitidea点击:

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

javaemailjavamailioexceptionjnotify

提问by Vidhee

Currently I'm inline of writing a code that will be listening to a directory. when the directory is updated with .apk file, I'll send a mail with this .apk file to a gmail account. I'm using Jnotify and JAVA Mail in my program.

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

The Error I'm getting is,

我得到的错误是,

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"

I looked for the solutions given in the stackoverflow for help but none of them where helpful.

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

Thanks in Advance

提前致谢

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("[email protected]", "senderPassword");
            }
        }
    );

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

    try{
        Message message = new MimeMessage(session); 
        message.setFrom(new InternetAddress("[email protected]"));
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("[email protected]"));
        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();
    }
}

采纳答案by Vidhee

This issue can get a solution by taking up the below two steps.

这个问题可以通过以下两个步骤来解决。

  1. Make sure java mail is 1.4.7.(Previosly I used 1.4.5 which led to all confusions). Download it from http://www.oracle.com/technetwork/java/index-138643.html
  2. Add this piece of code before sending the message, Thread.currentThread().setContextClassLoader( getClass().getClassLoader() );
  1. 确保 java 邮件是 1.4.7。(以前我使用 1.4.5 这导致了所有的混乱)。从http://www.oracle.com/technetwork/java/index-138643.html下载
  2. 在发送消息之前添加这段代码,Thread.currentThread().setContextClassLoader(getClass().getClassLoader());

回答by Bill Shannon

Tell me more about the environment in which your code is running. What JDK are you using? Are you running in an application server?

告诉我更多有关您的代码运行的环境的信息。你用的是什么JDK?您是否在应用程序服务器中运行?

The JavaBeans Activation Framework (JAF) looks for configuration files that tell how to map MIME types to the Java classes (DataContentHandlers) that handle them. It uses the ClassLoader to find the configuration files. If there are problems with the ClassLoader, the configuration files might not be found.

JavaBeans Activation Framework (JAF) 查找配置文件,这些文件说明如何将 MIME 类型映射到处理它们的 Java 类 (DataContentHandlers)。它使用 ClassLoader 来查找配置文件。如果 ClassLoader 出现问题,则可能找不到配置文件。

You might want to try the workaround described here, but of course it would be better to determine the root cause of the problem for you.

您可能想尝试此处描述的解决方法,但当然最好为您确定问题的根本原因。

Finally, you might want to simplify your program by fixing some of these common JavaMail mistakes.

最后,您可能希望通过修复一些常见的 JavaMail 错误来简化您的程序。

回答by Som

JavaMail depends on some configuration files to map MIME types to Java classes (e.g., multipart/mixedto javax.mail.internet.MimeMultipart). These configuration files are loaded using the ClassLoader for the application. If the ClassLoader doesn't function properly, these configuration files won't be found.

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

You can simply add below lines .. that solves the issue .

您只需添加以下几行即可解决问题。

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

回答by Aday

Adding the current thread before sending the email is the solution:

在发送电子邮件之前添加当前线程是解决方案:

Thread.currentThread().setContextClassLoader( getClass().getClassLoader() );

回答by slaman

If your build.xml does this: zipfileset src="javamail-1.4.7/mail.jar" excludes="META-INF/"**

如果您的 build.xml 这样做: zipfileset src="javamail-1.4.7/mail.jar" excludes="META-INF/"**

Then you're stripping out the configuration information.

然后您将剥离配置信息。

回答by Steven Swart

I am busy converting a Java 8 project to Java 10. At the same time I have been updating all of the dependencies. I was getting a similar exception and none of the above solutions worked for me.

我正忙于将 Java 8 项目转换为 Java 10。同时我一直在更新所有依赖项。我遇到了类似的异常,上述解决方案都不适合我。

I have the following in my pom.xml:

我的 pom.xml 中有以下内容:

<dependency>
    <groupId>com.sun.mail</groupId>
    <artifactId>javax.mail</artifactId>
    <version>1.6.1</version>
</dependency>

I did a bit more research and found the following link:

我做了更多的研究,发现了以下链接:

http://www.jguru.com/faq/view.jsp?EID=237257

http://www.jguru.com/faq/view.jsp?EID=237257

So I tried adding the following dependency to my pom.xml:

所以我尝试将以下依赖项添加到我的 pom.xml 中:

<dependency>
    <groupId>javax.activation</groupId>
    <artifactId>activation</artifactId>
    <version>1.1.1</version>
 </dependency>

That fixed the problem, I was able to send mail with attachments again.

这解决了问题,我能够再次发送带有附件的邮件。

回答by Q Locker

If it's a android project, it's highly possible the proguard stripped out unused classes by mistake, please add the following lines in the proguard file to fix the problem without modifying the code directly:

如果是android项目,很有可能proguard误删了未使用的类,请在proguard文件中添加以下几行解决问题,无需直接修改代码:

-keep class com.sun.mail.handlers.**
-dontwarn com.sun.mail.handlers.handler_base

回答by GeneralElektrix

Som's answer (MailcapCommandMap) worked for me with Spring war Portlets in Liferay 7.1 ga1. However, I had to remove Tomcat's mail.jarfrom tomcat/lib/extand replace it with javax.mail-1.6.2.jar, then make sure the dependency is scoped as provided in the project's pom.xml:

Som 的回答 ( MailcapCommandMap) 在 Liferay 7.1 ga1 中使用 Spring war Portlets 为我工作。但是,我不得不删除Tomcat的mail.jar距离tomcat/lib/ext,取而代之的是javax.mail-1.6.2.jar,请确保在项目的pom.xml所提供的相关性作用域:

<dependency>
    <groupId>com.sun.mail</groupId>
    <artifactId>javax.mail</artifactId>
    <version>1.6.2</version>
    <scope>provided</scope>
</dependency>

回答by Ian

The answer from Somworked for me. However I had to modify the mappings as I was using JavaMail DSN, and needed those mailcap entries also (included below, including Som's answer):

Som的回答对我有用。但是,我在使用 JavaMail DSN 时不得不修改映射,并且还需要这些 mailcap 条目(包括在下面,包括 Som 的回答):

// Original answer from Som:
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");

// Additional elements to make DSN work 
mc.addMailcap("multipart/report;;  x-java-content-handler=com.sun.mail.dsn.multipart_report");
mc.addMailcap("message/delivery-status;; x-java-content-handler=com.sun.mail.dsn.message_deliverystatus");
mc.addMailcap("message/disposition-notification;; x-java-content-handler=com.sun.mail.dsn.message_dispositionnotification");
mc.addMailcap("text/rfc822-headers;;   x-java-content-handler=com.sun.mail.dsn.text_rfc822headers");

As it turns out, it was adding the DSN JAR into my fat JAR (using shadowJar/Gradle) that caused the problem: the META-INF/mailcap from the DSN jar was overwriting the core one.

事实证明,是将 DSN JAR 添加到我的胖 JAR(使用 shadowJar/Gradle)中导致了问题:来自 DSN jar 的 META-INF/mailcap 覆盖了核心 JAR。

回答by metatechbe

Under OSGI, the following workaround allows the javax.activation bundle to load the "META-INF/mailcap" resource from the javax.mail bundle :

在 OSGI 下,以下解决方法允许 javax.activation 包从 javax.mail 包加载“META-INF/mailcap”资源:

Thread.currentThread().setContextClassLoader(javax.mail.Message.class.getClassLoader());

Note : character sets conversions may have limitations with this workaround...

注意:字符集转换可能会受到此解决方法的限制...