本文介绍了将EmailMessage转换为ByteArrayOutputStream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Java EWS API(EWS-Exchange Web服务)从Exchange服务器获取电子邮件,并将其存储在专有CMS中.我收到消息的类型是 microsoft.exchange.webservices.data.EmailMessage -EWS API提供的类.CMS API需要 ByteArrayOutputStream 对象作为其方法的参数.

I am fetching email from Exchange server using Java EWS API (EWS - Exchange Web Services) and storing it in a proprietary CMS. The type in which I am getting message is microsoft.exchange.webservices.data.EmailMessage - a class provided by EWS API. The CMS API requires ByteArrayOutputStream object as a parameter to its method.

所以我想将 EmailMessage 对象转换为 ByteArrayOutputStream .我看到了此线程,并尝试了类似的操作:( item 下面的类型为 EmailMessage )

So I want to convert EmailMessage object to ByteArrayOutputStream. I saw this thread and tried similar like this: (Below item is of type EmailMessage)

ByteArrayOutputStream b = new ByteArrayOutputStream();
try
{
    ObjectOutputStream o  = new ObjectOutputStream(b);
    o.writeObject((Object)item);
}
catch(IOException ioe)   
{
    ioe.printStackTrace(); 
}

但是我得到

java.io.NotSerializableException: microsoft.exchange.webservices.data.EmailMessage
     at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1180)   
     at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:346)

我能够使用 FileOutputStream 将这些 EmailMessage 对象保存为 .eml 格式,但是现在我无法找到解决方法将它们转换为 ByteArrayOutputStream .因此,有什么方法可以将 FileOutputStream 转换为 ByteArrayOutputStream ,或者直接将其从 EmailMessage 转换为 ByteArrayOutputStream .

I am able to save these EmailMessage objects in .eml format using FileOutputStream, however now I am not able to find the way to convert them to ByteArrayOutputStream.So is there any way to convert FileOutputStream to ByteArrayOutputStream or just directly from EmailMessage to ByteArrayOutputStream.

推荐答案

您会遇到此异常,因为您的(Object)item 的类未实现 Serializable 接口.从Java文档 writeObject(Object obj)

You are getting this exception because your (Object)item's class not implementing Serializable interface. From java doc writeObject(Object obj)

这篇关于将EmailMessage转换为ByteArrayOutputStream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 03:40