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

烂漫一生 提交于 2019-11-27 06:35:43

问题


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.

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.

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("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 depends on some configuration files to map MIME types to Java classes (e.g., multipart/mixed to 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.

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



回答2:


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



回答3:


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

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



回答4:


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.

I have the following in my 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

So I tried adding the following dependency to my 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.




回答5:


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:

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



回答6:


The answer from Som worked 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):

// 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.




回答7:


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

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

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




回答8:


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

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.

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.




回答9:


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

Then you're stripping out the configuration information.




回答10:


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

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


来源:https://stackoverflow.com/questions/21856211/javax-activation-unsupporteddatatypeexception-no-object-dch-for-mime-type-multi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!