有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

jakarta mail Java IMAP在写入EML时无休止地循环

我们的系统遇到了一个严重的问题,那就是处理来自IMAP的邮件并将其保存为EML。事情是,一切都在生产中工作,但不再在开发机器上工作,因此我们无法进行任何修复或进一步开发。这有点奇怪,因为这个例子也不起作用:

package com.test;

import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;

import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Store;

public class ReadMail {

    public static void main(String args[]) throws Exception {

        String host = "mail";
        String user = "xxx";
        String password = "zzz";
        Properties properties = System.getProperties();
        Session session = Session.getDefaultInstance(properties);
        Store store = session.getStore("imap");
        store.connect(host, user, password);
        Folder folder = store.getFolder("a");
        folder.open(Folder.READ_ONLY);

        Message[] message = folder.getMessages();

        for (int i = 0; i < message.length; i++) {

            System.out.println("------------ Message " + (i + 1) + " ------------");

            System.out.println(message[i].getSentDate());
            System.out.println(message[i].getFrom()[0]);
            System.out.println(message[i].getSubject());

            try (OutputStream out = new FileOutputStream("something"+i+".eml")) { // fix the name of course
                message[i].writeTo(out);
            }



        }

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

    }
}

在writeTo中,它进行无休止的循环并创建文件,该文件将一直增长到磁盘已满。这是来自com.sun.mail.imap.IMAPMessage

/**
 * Write out the bytes into the given OutputStream.
 */
public void writeTo(OutputStream os)
            throws IOException, MessagingException {
if (bodyLoaded) {
    super.writeTo(os);
    return;
}
InputStream is = getMimeStream();
try {
    // write out the bytes
    byte[] bytes = new byte[16*1024];
    int count;
    while ((count = is.read(bytes)) != -1)
    os.write(bytes, 0, count);
} finally {
    is.close();
}
}

它永远不会退出while子句。这应该是后缀方面的错误吗?邮件是多部分MIME消息,创建的EML只是EML的多个副本。prod和dev机器之间的唯一区别是硬件。JDK8是相同的。有什么提示吗

使用的Java邮件API:

    <dependency>
        <groupId>com.sun.mail</groupId>
        <artifactId>javax.mail</artifactId>
        <version>1.5.6</version>
    </dependency>

更新已解决:ESET Nod32防病毒软件以这种方式拦截IMAP流量,缓冲区永远不会返回-1!禁用ESET邮件后,一切正常


共 (0) 个答案