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