Reading a Multipart from an InputStream
843830Jan 29 2003 — edited Nov 1 2006I am having a problem rading a Multipart that was serialized to a file ( or other source ):
Take this code:
MimeMultipart part = new MimeMultipart();
MimeBodyPart body = new MimeBodyPart();
body.setText( "Attach 1" );
part.addBodyPart( body );
body = new MimeBodyPart();
body.setText( "Attach 2" );
part.addBodyPart( body );
part.writeTo( new FileOutputStream( "c:/workarea/mime-test.txt"));
part = new MimeMultipart( new FileDataSource( "c:/workarea/mime-test.txt"));
System.out.println( part.getCount() );
The file mime-test.txt contains the following:
------=_Part_0_2583282.1043901041089
Attach 1
------=_Part_0_2583282.1043901041089
Attach 2
------=_Part_0_2583282.1043901041089--
The above code will throw a "javax.mail.MessagingException: Missing start boundary" on the last line ... presumably because the start and boundary tags for the multipart is not really written out using the MimeMultipart.writeTo() method.
Even changing the file so that it contains:
Content-Type: multipart/mixed; boundary="------=_Part_0_2583282.1043901041089"
------=_Part_0_2583282.1043901041089
Attach 1
------=_Part_0_2583282.1043901041089
Attach 2
------=_Part_0_2583282.1043901041089--
.. and reload it with:
part = new MimeMultipart( new FileDataSource( "c:/workarea/mime-test.txt"));
System.out.println( part.getCount() );
.... will also throw the same Exception.
But the following code does NOT throw an Exception .... except that:
Multipart part = new MimeMultipart();
MimeBodyPart body = new MimeBodyPart();
body.setText( "Attach 1" );
part.addBodyPart( body );
body = new MimeBodyPart();
body.setText( "Attach 2" );
part.addBodyPart( body );
part.writeTo( new FileOutputStream( "c:/workarea/mime-test.txt"));
Session session = javax.mail.Session.getDefaultInstance( new java.util.Properties(), null );
MimeMessage message = new MimeMessage( session );
body = new MimeBodyPart( new FileInputStream( "c:/workarea/mime-test.txt"));
part = new MimeMultipart();
part.addBodyPart( body );
System.out.println( part.getCount());
part.writeTo( new FileOutputStream( "c:/workarea/mime-test2.txt"));
.. the output in mime-test2.txt is:
------=_Part_1_1549180.1043900077808
------=_Part_0_7686866.1043900077339
Attach 1
------=_Part_0_7686866.1043900077339
Attach 2
------=_Part_0_7686866.1043900077339--
------=_Part_1_1549180.1043900077808--
... which is now 1 Bodypart containing a multipart that has 2 parts ... not the same as the original file in mime-test.txt
Any ideas?