为什么使用IMAP而不使用POP3时JavaMail BodyPart.getInputStream()返回空流? [英] Why does JavaMail BodyPart.getInputStream() return an empty stream when using IMAP, but not when using POP3?

查看:493
本文介绍了为什么使用IMAP而不使用POP3时JavaMail BodyPart.getInputStream()返回空流?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个javax.mail应用程序,该应用程序通过电子邮件进行解析并获取所有application/*附件的InputStream:

I have a javax.mail application that parses through emails and gets the InputStream for all application/* attachments:

private DataInputStream getAttachmentStream(Message message) throws MessagingException, IOException {
    if (message.isMimeType("multipart/*")) {
        Multipart mp = (Multipart) message.getContent();

        for (int p = 0; p < mp.getCount(); p++) {
            BodyPart part = mp.getBodyPart(p);

            if (part.getContentType().toLowerCase().startsWith("application")) {
                InputStream is = part.getInputStream();

                DataInputStream dis = new DataInputStream(is);

                App.logger.info("Found attachment."");
                return dis;
            }
        }
    }

    App.logger.warn("No attachment found.");
    return null;
}

我的问题是,即使对于具有附件的电子邮件,结果DataInputStream也为空.我已经调试了整个调试器,并且part绝对是带有附件的正确的Message部分.

My problem is that even for emails that have an attachment, the resultant DataInputStream is empty. I've stepped through in the debugger, and part is definitely the correct Message part with the attachment.

我切换了检查电子邮件地址的代码协议以使用POP3而不是IMAP,并且此代码神奇地起作用了.谁能解释为什么此代码适用于POP3而不适用于IMAP?

推荐答案

自该线程打开以来已经有一段时间了,但是我认为这里描述的问题是由于IMAP服务器的部分获取实现中的错误所致.如此链接所述 http://www.oracle.com/technetwork/java/faq-135477.html#fetch ,并在这些注释中 https://javamail. java.net/docs/NOTES.txt .有一种解决方法,添加以下属性:

It has been some time since this thread was opened, but I think the problem described in here was due to bugs in partial fetch implementation of IMAP server. As described in this link http://www.oracle.com/technetwork/java/faq-135477.html#fetch and in these notes https://javamail.java.net/docs/NOTES.txt . There is a workaround to solve it, add the following property:

props.setProperty("mail.imap.partialfetch","false");

props.setProperty("mail.imap.partialfetch", "false");

这篇关于为什么使用IMAP而不使用POP3时JavaMail BodyPart.getInputStream()返回空流?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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