不同操作系统下JavaMail中part.getContent的类型是什么? [英] What is the type for part.getContent in JavaMail under different os?

查看:444
本文介绍了不同操作系统下JavaMail中part.getContent的类型是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JavaMail接收邮件. 首先,我是在Mac OS X上进行开发的.从Internet上找到的示例看起来像这样:

public void getMailContent(Part part) throws Exception {   
        String contenttype = part.getContentType();   
        int nameindex = contenttype.indexOf("name");   
        boolean conname = false;   
        if (nameindex != -1)   
            conname = true;   
        System.out.println("CONTENTTYPE: " + contenttype);   
        if (part.isMimeType("text/plain") && !conname) {   
            bodytext.append((String) part.getContent());   
        } else if (part.isMimeType("text/html") && !conname) {   
            bodytext.append((String) part.getContent());   
        } else if (part.isMimeType("multipart/*")) {   
            Multipart multipart = (Multipart) part.getContent();   
            int counts = multipart.getCount();   
            for (int i = 0; i < counts; i++) {   
                getMailContent(multipart.getBodyPart(i));   
            }   
        } else if (part.isMimeType("message/rfc822")) {   
            getMailContent((Part) part.getContent());   
        } else {}   
    }

但是我发现它不起作用.返回值是InputStream的扩展. 所以我用它来解决问题.

InputStreamReader isr = new InputStreamReader((InputStream) part.getContent(), language);
BufferedReader br = new BufferedReader(isr);
String line = null;
StringBuilder sb = new StringBuilder();
while ((line = br.readLine()) != null) {
    sb.append(line).append("\n");
 }
result = MimeUtility.decodeText(sb.toString());

但是最近,我有一台新电脑,并在Windows 7下运行上面的代码,它也无法正常工作.例外是java.lang.String cannot be cast to java.io.InputStream.part.getContent()返回一个String作为Internet上的示例./p>

我只是不知道原因.而且如何在Mac和Windows上正常运行或如何避免此问题并获取零件内容的任何方式.

谢谢.

解决方案

我遇到了同样的问题,找到了原因(尽管我想,这不一定是可能导致问题的唯一原因),并修复了它,所以我决定发布解决方案.

就我而言,我正在构建Android应用程序.在Android中,其javax.security程序包中缺少某些类,因此 asmack 帮助程序包,并且JavaMail库是从源代码构建的.

问题是,我没有将所需的资源打包到javamail jar中.具体来说,导出jar时应打包以下文件:

  • dsn.mf
  • javamail.charset.map
  • javamail.default.address.map
  • javamail.default.providers
  • javamail.imap.provider
  • javamail.pop3.provider
  • javamail.smtp.address.map
  • javamail.smtp.provider
  • mailcap

修复后,仅通过getContent方法就可以正确解码内容.在此页面上找到了线索.

I am using JavaMail to receive mails. At first,I develop under Mac OS X.The example I found from Internet seems like this:

public void getMailContent(Part part) throws Exception {   
        String contenttype = part.getContentType();   
        int nameindex = contenttype.indexOf("name");   
        boolean conname = false;   
        if (nameindex != -1)   
            conname = true;   
        System.out.println("CONTENTTYPE: " + contenttype);   
        if (part.isMimeType("text/plain") && !conname) {   
            bodytext.append((String) part.getContent());   
        } else if (part.isMimeType("text/html") && !conname) {   
            bodytext.append((String) part.getContent());   
        } else if (part.isMimeType("multipart/*")) {   
            Multipart multipart = (Multipart) part.getContent();   
            int counts = multipart.getCount();   
            for (int i = 0; i < counts; i++) {   
                getMailContent(multipart.getBodyPart(i));   
            }   
        } else if (part.isMimeType("message/rfc822")) {   
            getMailContent((Part) part.getContent());   
        } else {}   
    }

But I found it don't work.The return value is a extends of InputStream. So I use this to solve the problem.

InputStreamReader isr = new InputStreamReader((InputStream) part.getContent(), language);
BufferedReader br = new BufferedReader(isr);
String line = null;
StringBuilder sb = new StringBuilder();
while ((line = br.readLine()) != null) {
    sb.append(line).append("\n");
 }
result = MimeUtility.decodeText(sb.toString());

But recently,I got a new pc and run the code above under Windows 7,It don't work also.The exception is java.lang.String cannot be cast to java.io.InputStream.The part.getContent() returns a String as the example on internet.

I just don't know the reason.And how to run is properly on both mac and windows or any way to avoid this issue and get the content of the part.

Thanks.

解决方案

I faced the same problem, found the reason (though, I suppose, this is not necessarily the only reason that may lead to the problem), and fixed it, so I decided to post the solution.

In my case, I was building Android application. In Android, some classes are missing in its javax.security package, so the asmack helper package was used, and JavaMail library has been built from sources.

The problem was, that I did not pack required resources into the javamail jar. Specifically, the following files should be packed while exporting the jar:

  • dsn.mf
  • javamail.charset.map
  • javamail.default.address.map
  • javamail.default.providers
  • javamail.imap.provider
  • javamail.pop3.provider
  • javamail.smtp.address.map
  • javamail.smtp.provider
  • mailcap

After the fix, I'm getting properly decoded content just from the getContent method. The clue was found on this page.

这篇关于不同操作系统下JavaMail中part.getContent的类型是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆