gpt4 book ai didi

JavaMail 问题或错误?

转载 作者:行者123 更新时间:2023-12-01 17:40:57 32 4
gpt4 key购买 nike

我有一个关于 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));
}
}

最佳答案

JavaMail API 是一头 pig ,直接使用它是一项吃力不讨好的任务,主要是因为它的行为并不符合您的预期。

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

关于JavaMail 问题或错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/599188/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com