当'mail.pop3.filecache.enabled'为true时,抛出Stream Closed异常

时间:2012-01-02 12:47:40

标签: java file-io javamail pop3

我编写了一个基于Java的独立程序(使用JavaMail API v1.4.4)来读取来自POP3服务器的电子邮件,我还将属性mail.pop3.filecache.enable设置为 true 使用:

props.put("mail.pop3.filecache.enable", "true");

对于特定的电子邮件,我从Stream Closed方法获得以下运行时message.getContent()异常:

Exception stracktrace:

java.lang.RuntimeException: Stream closed
    at javax.mail.util.SharedFileInputStream.newStream(SharedFileInputStream.java:507)
    at com.sun.mail.pop3.POP3Message.getContentStream(POP3Message.java:242)
    at javax.mail.internet.MimePartDataSource.getInputStream(MimePartDataSource.java:98)
    at com.sun.mail.handlers.text_plain.getContent(text_plain.java:107)
    at javax.activation.DataSourceDataContentHandler.getContent(DataHandler.java:774)
    at javax.activation.DataHandler.getContent(DataHandler.java:521)
    at javax.mail.internet.MimeMessage.getContent(MimeMessage.java:1419)
    at ReadPop3Email.readPOP3Account(ReadPop3Email.java:290)
    at ReadPop3Email.main(ReadPop3Email.java:73)

ReadPop3Email.java 的第290行是以下内容:

Object object = message.getContent();

但是,如果我将mail.pop3.filecache.enable设置为 false ,我看不到任何异常。但是,我们希望将属性mail.pop3.filecache.enable用于 true ,因为它会根据JavaMail doc将消息数据缓存在临时文件而不是内存中。

启用filecache时,为什么会出现此异常?出了什么问题?任何帮助/建议都非常感谢。我正在使用JDK1.6,如果这很重要的话。

修改 虽然我无法共享/发布原始源代码,但我已粘贴到原始源代码的修改版本下面。

public class ReadPop3Email
{

    public static void main(String[] args)
    {
        System.setProperty("mail.mime.parameters.strict", "false");
        System.setProperty("mail.mime.ignoremultipartencoding", "false");

        ReadPop3Email pop3Email = new ReadPop3Email();
        pop3Email.readPOP3Account();
    }

    public static String[] getPOP3Info()
    {
        return new String[]{"mypop3host.net", "110", "jack@example.net", "password"};
    }

    public Properties getPOP3Properties()
    {
        Properties props = new Properties();
        props.put("mail.pop3.timeout", "30000");
        props.put("mail.pop3.connectiontimeout", "10000");
        props.put("mail.pop3.filecache.enable", "true");
        return props;
    }

    public void readPOP3Account()
    {
        String server = getPOP3Info()[0];
        int port = Integer.parseInt(getPOP3Info()[1]);
        String username = getPOP3Info()[2];
        String password = getPOP3Info()[3];
        Session session = null;
        Store store = null;
        Folder folder = null;
        POP3Folder pf = null;
        Message message = null;
        Message[] messages = null;
        try {
            System.out.println("-------------- Started processing POP3 account: " + username + " -----------------");
            session = Session.getInstance(getPOP3Properties());

            System.out.println("Getting the session for accessing email.");
            store = session.getStore("pop3");

            store.connect(server, port, username, password);
            System.out.println("Connection established with POP3 server.");

            folder = store.getDefaultFolder();

            folder = folder.getFolder("inbox");
            System.out.println("Getting the Inbox folder.");

            folder.open(Folder.READ_ONLY);

            pf = (POP3Folder) folder;
            System.out.println("Total messages found: " + folder.getMessageCount());

            messages = folder.getMessages();

            //Message no. 26 is the one having this issue
            message = messages[26 - 1];

            System.out.println("Message number: " + message.getMessageNumber());
            System.out.println("Message UID: " + pf.getUID(message));
            System.out.println("From: " + message.getFrom()[0].toString());
            System.out.println("Subject: " + message.getSubject());
            System.out.println("Received date: " + message.getReceivedDate());
            System.out.println("Sent date: " + message.getSentDate());
            System.out.println("Content Type: " + message.getContentType());
            System.out.println("Disposition: " + message.getDisposition());
            boolean isMultipart = (message.getContent() instanceof Multipart);
            System.out.println("Message is multipart? " + isMultipart);

            if (!isMultipart) {
                getBodyContent(message);
            }

            folder.close(true);
            store.close();

            System.out.println("-------------- Completed processing POP3 account: " + username + " -----------------");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private void getBodyContent(Message message)
    {
        try 
        {
            System.out.println("============== BODY CONTENT STARTS HERE ==============");
            Object object = message.getContent();
            if (object instanceof String) {
                System.out.println("STRING content follows below: \n" + (String) object);
            } else if (object instanceof InputStream) {
                System.out.println("INPUTSTREAM content");
                InputStream is = (InputStream) object;
                int c;
                while ((c = is.read()) != -1) {
                    System.out.write(c);
                }
            }
            System.out.println("============== BODY CONTENT ENDS HERE ==============");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

1 个答案:

答案 0 :(得分:0)

请记住,当文件夹关闭或JVM终止时,文件缓存将被删除。