Javamail -- IMAP & Multipart/Alternative

T

Tajonis

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.
 
T

Thomas Weidenfeller

Tajonis said:
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
 
C

Chris Uppal

Tajonis said:
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
 
T

Tajonis

Thomas said:
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

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.
 
T

Tajonis

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top