MSG Output

166 views
Skip to first unread message

eric

unread,
Jan 17, 2011, 12:50:03 PM1/17/11
to java-libpst-discuss
I've been looking through the classes, but i'm unable to find a way to
take a given PSTObject and save it to disk. In either MSG or EML
format (complete with attachments).

Does this exist yet in this library? Or maybe someone has a
suggestion for a library that can help me save these to disk. Thanks!

-Eric

Dimitry Erastov

unread,
Jan 25, 2012, 9:41:59 PM1/25/12
to java-libp...@googlegroups.com
Has anyone found a way to do it? I'm currently facing the same problem and thinking to go like this:

// read pstMessage from pstFile
PSTNodeInputStream stream = new PSTNodeInputStream(pstFile, pstMessage.getDescriptorNode());
// write to output stream

The only problem is that PSTMessage doesn't expose its PSTFile. I'll either have to edit the source code or extend PSTMessage to get access to its protected file field.

Ben Reid

unread,
Jun 28, 2012, 12:17:59 AM6/28/12
to java-libp...@googlegroups.com
Hey Dimitry,

Did you get this working...?

I have not yet figured out how to save the message detail into an MSG, EML or MHT file.

I am successfully getting the Text, HTML and RTF content where available, but there does not seem to be any function that gets you the content in a format that can be written to MSG, EML or MHT - unless your method works?

Dmitry Erastov

unread,
Jun 28, 2012, 4:29:47 AM6/28/12
to java-libp...@googlegroups.com, Ben Reid
Unfortunately, just as the author states in one of the issues, there's no supported way of doing it.
I ended up reconstructing a MIME message by accessing individual PSTMessage properties. In my case the reconstruction was approximate, but accurate enough.
The stream that I mention in the previous message actually doesn't contain MIME-formatted data, so that method won't work.
Message has been deleted

Dimitry Erastov

unread,
Jun 29, 2012, 9:53:57 PM6/29/12
to java-libp...@googlegroups.com, Ben Reid
I can't share any literal code without an OK from my employer's open source group (argh!), but here's the general overview of the process.

0. Create a new MimeMessage.
1. Copy sender/recipient addresses from the pst message.
2. Copy over any necessary headers (not all of them are directly transferable to an RFC 822 message).
3. Check whether a pst message has a plain text and/or an html body.
 3.1. If it only has one of them, create a MimeBodyPart with the body text (I always specify "utf-8" fro the encoding and haven't seen any problems with that).
 3.2. If it has both, create a new MimeMultipart("alternative") and attach both text parts to it.
 3.3. If the message has attachments, create a new MimeMultipart("mixed") - this will be the root message part. Otherwise the root will be either 3.1 or 3.2.
 3.4. If you had to create a part from 3.2, you'll need to wrap it into a MimeBodyPart and attach to the root part from 3.3. Otherwise just attach the text part from 3.1 to the root.
 3.5. Now loop through all the attachments. Link them to the root part.
  3.5.1. Basically you create a MimeBodyPart, copy any necessary properties from the pst attachment, get the input stream.
  3.5.2. For all content types except the plain text, use the stream directly. For text, you'll need to create a String out of it and set that as a part content (again use utf-8). This is probably related to your problem with text attachments.
4. Set message mime type according to the type of the root part.
5. Call message.saveChanges() to make sure headers are consistent with the content.

Hope this helps.

One important thing to remember is that any calls to any of the Pst* classses can potentially throw RuntimeException's (and they often do), so client code has to be written accordingly. I've made some fixes to the library to fix the most notorious of those exceptions and am now working with my open source team to get permission to release those fixes back to the community. Hopefully it will happen next week.

Regards,
Dmitry


On Thu, Jun 28, 2012 at 2:03 AM, Ben Reid wrote:

Can I ask how you reconstructed the MIME message?

 Even if it was approximate…  I may be able to use the same.

 I already have all the PSTMessage properties available to me obviously. Although I have modified the java files and recompiled the class and jar files to add some missing functionality for getOriginalSubmitTime() and getAttachEncoding().

 Another question for you… I have had trouble saving off plain text file attachments… they come out garbled. All the binary attachments are fine (PNG, GIF, DOC, etc) – just the plain text ones are garbled. Should I be using some other method other than getFileInputStream()?

 I tried writing them using a standard FileInputStream and FileOutputStream (like I do for the binary attachments) and even tried writing using java.io.FileWriter with no luck. I am also only able to determine if it is text based on the file extension as there does not appear to be a property for the mime-type or content-disposition.

 Any help you could offer on these two topics would be sincerely appreciated, as it is the last piece of my puzzle.

I can re-post this to the list if you think I should.

 

Kind regards,

Ben Reid
Reply all
Reply to author
Forward
0 new messages