2

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.

Community
  • 1
  • 1
Mahesha999
  • 22,693
  • 29
  • 116
  • 189
  • If you can write it out on the source machine, maybe you can use `FileInputStream` to read it in and convert that to `ByteArrayOutputStream` like the example here: http://stackoverflow.com/a/19689394/151234 – Brendan Hannemann Oct 31 '13 at 16:36
  • Yes actually I tried it like - fetching mail from exchange - saving it locally - moving to repository from local path, but now I want to handle this all in-memory, I mean I dont want to store mails on local drive. Thats why I want to convert it directly from `EmailMessage`, or to be precise without calling `write()` on `FileOutputStream` – Mahesha999 Oct 31 '13 at 19:29

1 Answers1

0

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

Exceptions are thrown for problems with the OutputStream and for classes that should not be serialized. All exceptions are fatal to the OutputStream, which is left in an indeterminate state, and it is up to the caller to ignore or recover the stream state.

Sumit Singh
  • 15,743
  • 6
  • 59
  • 89