I am having some issues when using Javamail and an IMAP folder. I am
trying to check for an IMAP folder for specific messages (this part is
working great). When I notice a message I am interested in I begin to
process the message and hopefully save the contents to disk. What I am
seeing is that if the message is a multipart/alternative message my
applications processes the message but the resulting file(s) are all
empty. Here is my method that is handling the multipart messages
private void processMultpartMessage(Multipart part,String filename)
throws MessagingException,IOException{
ArrayList contents = new ArrayList();
for(int index = 0; index < part.getCount(); index++){
BodyPart bodyPart = part.getBodyPart(index);
Object content = bodyPart.getContent();
InputStream input = bodyPart.getInputStream();
byte[] contentArray = new byte[input.available()];
input.read(contentArray);
contents.add(contentArray);
input.close();
}
int partCounter = 1;
if(!contents.isEmpty()){
Iterator iterator = contents.iterator();
while(iterator.hasNext()){
byte[] content = (byte[])iterator.next();
writePartToFile(filename,content,partCounter);
partCounter++;
}
}
}
Is there something I am missing here or am I just stoopid ;-)
As a side note this all works fine if I am using a POP folder to
recieve mail.
Thomas Weidenfeller - 23 Feb 2006 08:16 GMT
> Is there something I am missing here or am I just stoopid ;-)
Use a debugger. If you don't know how to operate one, this is a good
opportunity to learn it. My guess is on the input.available() call
returning 0. Copy the data the normal way and forget about using
available().
/Thomas

Signature
The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/
Tajonis - 23 Feb 2006 13:25 GMT
> > Is there something I am missing here or am I just stoopid ;-)
>
[quoted text clipped - 8 lines]
> ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
> http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/
I do have plenty experience with a debugger and your guess would be
correct input.available() is returing 0. However if you use
bodyPart.writeTo(System.out) you can see the data handler's content
properly but for some reaso you just can not read from it.
Chris Uppal - 23 Feb 2006 12:27 GMT
> byte[] contentArray = new byte[input.available()];
There are very few valid uses for available(), this isn't one of them. Unless
you /really/ know what you are doing, /and/ have rather an unusual requirement
to satisfy, just don't use it. Ever.
> input.read(contentArray);
/Never/ ignore the return value from read(array)! /NEVER/
-- chris
Tajonis - 23 Feb 2006 13:30 GMT
You are right I should never ignore the return value of a read but
since input.available() is 0 thr result of read is also going to be 0.
I put in a very cheesy band aid for now to get past this problem.
if(input.available() == 0){
ByteArrayOutputStrean stream = new ByteArrayOutputStream();
bodyPart.writeTo(stream);
writePartToFile(filename,stream.toByteArray(),0);
}
The above works fine but I was not satisfied after digging around I
found something that mentioned MS Exchange/ Outlook and TNEF and I
think this might be the underlying culprit.