Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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("[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();     } } 
like image 747
Vidhee Avatar asked Feb 18 '14 14:02

Vidhee


2 Answers

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");  
like image 161
Som Avatar answered Sep 19 '22 08:09

Som


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

  1. Make sure java mail is 1.4.7.(Previously 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() ); 
like image 21
Vidhee Avatar answered Sep 21 '22 08:09

Vidhee