reading MimeMessages from file problem

Hi,

I have a problem with reading MimeMessages from file.

This method save all MimeMessages into file:

publicvoid saveToFile(){

Vector<MimeMessage> file=new Vector();

if (!file.isEmpty())

soubor.removeAllElements();

for (int i = 0; i < messages.length; i++){

file.addElement(messages[i]);

}

try{

FileOutputStream ostream =new FileOutputStream("data.sav");

ObjectOutputStream os =new ObjectOutputStream(ostream);

for (int i = 1; i < file.lastIndexOf(file.lastElement()); i++){

[b] file.get(i).writeTo(os);[/b]

}

}

catch (Exception e){e.printStackTrace();}

}

and this method is trying to load Mimemessages from file:

publicvoid loadFile(){

try{

FileInputStream istream =new FileInputStream("data.sav");

ObjectInputStream is =new ObjectInputStream(istream);

try{

-->file = (Vector<MimeMessage>) is.readObject();

}catch (IOException ex){

ex.printStackTrace();

}catch (ClassNotFoundException ex){

ex.printStackTrace();

}

}catch(IOException e){}

}

in marked text (- >)-> OptionalDataException

How can I load messages from file? I would like to save these MimeMessages to MimeMessage[] object

[2658 byte] By [LukasCa] at [2007-11-27 4:49:37]
# 1
Why are you using Object streams?The writeTo method writes a simple MIME byte stream, not an object stream.To read the data back in, use the MImeMessage constructor that takes anInputStream.
bshannona at 2007-7-12 10:02:45 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2

thank you,

but I am still confused. I saved many mails in 1 file with ByteArrayOutputStream, but how can I get them back all with MimeMessage constructor? MimeMessage object is only one email, but I need array MimeMessage[].

When I try this:

FileInputStream istream = new FileInputStream("data.sav");

MimeMessage recoverdMail=new MimeMessage((Session) null,istream);

it doesn't work - it's only one message, but I need many.

Thanks for your advice

LukasCa at 2007-7-12 10:02:45 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3

That's more work.

The simplest approach might be to use one of the "local store providers"

listed on the JavaMail Third Party Products page.

If you want to do this yourself, you're going to need to come up with a

file format that allows you to determine where one message ends and

the next message begins. Then, you're going to have to separate the

messages and create a separate InputStream for each one to construct

each MimeMessage object, collecting them all together into an array of

MimeMessage objects.

bshannona at 2007-7-12 10:02:45 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...