Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2006

Tip: Looking for answers? Try searching our database.

Javamail -- IMAP & Multipart/Alternative

Thread view: 
Tajonis - 22 Feb 2006 23:14 GMT
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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.