首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >javax.activation.UnsupportedDataTypeException: MIME类型multipart/mixed没有对象DCH;边界

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

Stack Overflow用户
提问于 2014-02-18 22:05:37
回答 9查看 44.7K关注 0票数 29

目前我正在内联编写一段代码,该代码将监听一个目录。当目录用.apk文件更新时,我会用这个.apk文件给一个gmail帐户发送一封邮件。我在我的程序中使用了Jnotify和JAVA Mail。

我得到的错误是,

代码语言:javascript
复制
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中给出的解决方案来寻求帮助,但它们都没有帮助。

提前感谢

代码语言:javascript
复制
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();
    }
}
EN

回答 9

Stack Overflow用户

发布于 2014-09-04 01:10:07

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

您只需添加以下行即可。这就解决了问题。

代码语言:javascript
复制
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"); 
票数 41
EN

Stack Overflow用户

发布于 2015-11-13 01:25:58

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

代码语言:javascript
复制
Thread.currentThread().setContextClassLoader( getClass().getClassLoader() );
票数 7
EN

Stack Overflow用户

发布于 2018-06-27 12:46:27

我正忙于将一个Java 8项目转换为Java 10,同时我一直在更新所有的依赖项。我得到了一个类似的异常,上面的解决方案对我都不起作用。

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

代码语言:javascript
复制
<dependency>
    <groupId>com.sun.mail</groupId>
    <artifactId>javax.mail</artifactId>
    <version>1.6.1</version>
</dependency>

我做了更多的研究,找到了以下链接:

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

因此,我尝试向我的pom.xml添加以下依赖项:

代码语言:javascript
复制
<dependency>
    <groupId>javax.activation</groupId>
    <artifactId>activation</artifactId>
    <version>1.1.1</version>
 </dependency>

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

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21856211

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档