Thursday, June 03, 2004

Copying RTF messages from Mailboxes to Public Folders

Copying messages from a mailbox to a public folder programmatically has never been a really easy task in Exchange, copying within one mailbox is okay you can use the basic ADO copy record and move record but between mailboxes or mailbox to public folder you start to hit some issues.

One method that I found that works is to use the message stream object, this gets you a serialized version of the message that is easier to deal with. The problem with this method is that if the mail has any Rich Text formatting (or any other custom MAPI properties) then these properties don't get copied with the stream (what usually happens is the email just gets set to HTML). To solve this problem you need to look at which MAPI properties you need to copy over and copy these manually in your code. For example my problem was that i needed to copy the RTF formatting of a message over when I copied an item between a mailbox and the public folder. Using MDBvu32 to examine a message I found the MAPI property PR_RTF_COMPRESSED which contains the RTF version of the message text. You can use this property in the field object of a message by using its Hex value which looks like http://schemas.microsoft.com/mapi/proptag/x10090102. So combining this with the following script solved my problem Eg.

set msgobj = createobject("CDO.Message")
set msgobj1 = createobject("CDO.Message")
set stm = CreateObject("ADODB.Stream")
msgobj.datasource.open "file://./backofficestorage/yourdomain.com/MBX/yourmailbox/inbox/email.eml",,3
set stm = msgobj.getstream()
msgobj1.datasource.openobject stm, "_Stream"
rtfbody = msgobj.fields("http://schemas.microsoft.com/mapi/proptag/x10090102")
msgobj1.fields("http://schemas.microsoft.com/mapi/proptag/x10090102") = rtfbody
msgobj1.fields("http://schemas.microsoft.com/exchange/outlookmessageclass") ="IPM.NOTE"
msgobj1.fields.update
msgobj1.datasource.savetocontainer "file://./backofficestorage/yourdomain.com/public folders/test/"