JavaMail问题还是Bug?

时间:2009-03-01 03:25:34

标签: java mime javamail parsing

我有一个关于Java Mail以及它如何与流一起工作的问题。在Java Mail 1.4.1中有一个MimeMessage constructor that accepts a stream。我的理解是我可以将一个流传递给这个构造函数,它会将它解析为MimeMessage。我写了两个测试来证明这一点。第一个测试发送的流只包含部分多部分MIME消息。第二个测试发送一个包含2个完整的多部分MIME消息的流。都没有像我预期的那样工作。第一个不抛出异常,第二个以某种方式将整个流读入单个消息。这是Java Mail中的错误还是我使用了错误的流?或者我错过了更大的东西?

这有点长,但这是测试代码:

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.mail.BodyPart;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.util.ByteArrayDataSource;

import junit.framework.TestCase;


public class mimeTest extends TestCase {

    public void testPartialMulitpartMessage() throws MessagingException, IOException 
    {
        Properties props = new Properties();
        Session session = Session.getInstance(props, null);
        String testMsg1 = "test";
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

        // Step 1 - Create first MIME message
        MimeMessage mesg = new MimeMessage(session);
        Multipart mp = new MimeMultipart("mixed");
        //create a child part
        BodyPart bodyPart = new MimeBodyPart();
        bodyPart.setContent(testMsg1, "application/x-special");
        bodyPart.setHeader("Content-Length", String.valueOf(testMsg1.length()));
        DataSource ds = new ByteArrayDataSource(testMsg1, "application/x-special");
        bodyPart.setDataHandler(new DataHandler(ds));
        bodyPart.setHeader("Content-Transfer-Encoding", "8bit");
        // Add the child part to the multipart
        mp.addBodyPart(bodyPart);
        // Put the MultiPart into the Message
        mesg.setContent(mp);

        // Step 2 - write to a stream
        mesg.writeTo(byteArrayOutputStream);

        byte bytes[] = byteArrayOutputStream.toByteArray();
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes, 0, 10);
        BufferedInputStream bufferedInputStream = new BufferedInputStream(byteArrayInputStream);
        boolean thrown = false;
        try
        {
            //Why does this not throw a messageexception.
            MimeMessage mesg2 = new MimeMessage(session, bufferedInputStream);
        }
        catch(MessagingException me){
            thrown = true;
        }

        if(!thrown) {
            assertTrue("Expected exception not thrown.", false);
        }
    }

    public void testMulitpleMulitpartMessages() throws MessagingException, IOException {
        Properties props = new Properties();
        Session session = Session.getInstance(props, null);
        String testMsg1 = "test";
        String testMsg2 = "test1";
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

        // Step 1 - Create first MIME message
        MimeMessage mesg = new MimeMessage(session);
        Multipart mp = new MimeMultipart("mixed");
        //create a child part
        BodyPart bodyPart = new MimeBodyPart();
        bodyPart.setContent(testMsg1, "application/x-special");
        bodyPart.setHeader("Content-Length", String.valueOf(testMsg1.length()));
        DataSource ds = new ByteArrayDataSource(testMsg1, "application/x-special");
        bodyPart.setDataHandler(new DataHandler(ds));
        bodyPart.setHeader("Content-Transfer-Encoding", "8bit");
        // Add the child part to the multipart
        mp.addBodyPart(bodyPart);
        // Put the MultiPart into the Message
        mesg.setContent(mp);

        // Step 2 - write to a stream
        mesg.writeTo(byteArrayOutputStream);

        // Step 3 - Create second MIME message
        MimeMessage mesg2 = new MimeMessage(session);
        mp = new MimeMultipart("mixed");
        //create a child part
        bodyPart = new MimeBodyPart();
        bodyPart.setContent(testMsg2, "application/x-special");
        bodyPart.setHeader("Content-Length", String.valueOf(testMsg2.length()));
        ds = new ByteArrayDataSource(testMsg2, "application/x-special");
        bodyPart.setDataHandler(new DataHandler(ds));
        bodyPart.setHeader("Content-Transfer-Encoding", "8bit");
        // Add the child part to the multipart 
        mp.addBodyPart(bodyPart);
        // Put the MultiPart into the Message
        mesg2.setContent(mp);

        // Step 4 - write to the same stream
        mesg2.writeTo(byteArrayOutputStream);

        // Step 6 - read the two messages back
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
        BufferedInputStream bufferedInputStream = new BufferedInputStream(byteArrayInputStream);
        List<MimeMessage> listMessages = new ArrayList<MimeMessage>();
        while (bufferedInputStream.available() > 0) {
            //http://java.sun.com/products/javamail/javadocs/javax/mail/internet/MimeMessage.html#MimeMessage(javax.mail.Session,%20java.io.InputStream)
            //The InputStream will be left positioned at the end of the data for the message.
            //WHY does this not work?  It reads the whole stream.
            mesg = new MimeMessage(session, bufferedInputStream);
            //output the message
            listMessages.add(mesg);
        }

        assertEquals(2, listMessages.size());

        assertTrue(listMessages.get(0).equals(mesg));
        assertTrue(listMessages.get(1).equals(mesg2));
    }
}

2 个答案:

答案 0 :(得分:4)

JavaMail API是一只猪,并且使用raw是一项吃力不讨好的任务,因为它的行为并不像你期望的那样。

我建议在它周围使用Spring的API层,压力要小得多。它并没有完全隐藏JavaMail,它只是使它更具可预测性和测试友好性。

答案 1 :(得分:1)

JavaMail将读取整个流,可能会缓冲它。我同意@skaffman,但我的观点是,它只是在没有评论原因的情况下进行了大量优化,这使得它很难使用。

你正在处理MimeMessage.parse(InputStream)方法。它会读取整个流,而不会关闭它,除非您使用的是SharedInputStream。正如您所看到的,它会将InputStream放在最后。

MimeMessage对解释流中的数据没有太大作用,因此我认为你“错过了更大的东西”来使用你的文字。为什么你期望它解释数据?你能发送你用来创建流的多部分消息吗?