微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

我得到的错误是,
javax.mail.MessagingException:IOException同时发送消息;
嵌套异常是:
javax.activation.UnsupportedDataTypeException:无对象DCH for MIME类型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类(例如,“maultipart / mixed”到“javax.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");

原文地址:https://www.jb51.cc/java/122076.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐