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.
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();
}
But I am getting
java.io.NotSerializableException: microsoft.exchange.webservices.data.EmailMessage
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1180)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:346)
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.
FileInputStreamto read it in and convert that toByteArrayOutputStreamlike the example here: stackoverflow.com/a/19689394/151234EmailMessage, or to be precise without callingwrite()onFileOutputStream