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 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") "file://./backofficestorage/",,3
set stm = msgobj.getstream()
msgobj1.datasource.openobject stm, "_Stream"
rtfbody = msgobj.fields("")
msgobj1.fields("") = rtfbody
msgobj1.fields("") ="IPM.NOTE"
msgobj1.datasource.savetocontainer "file://./backofficestorage/ folders/test/"


Anonymous said...

This idea is somewhat similar to what I want to accomplish. I want to have an (XML) formatted e-mail come into a mailbox. I need to process that e-mail and turn it into a Contact in a public folder. Have you done anything similar to this, or do you have any suggestions on where to start?

Glen said...

You could use a Store event sink to do this it would be easier if the mail was formated as a Vcard but XML should be okay just would require more lines of code. The place to start with Store event sinks is the Exchange SDK

MikeB said...

Is it possible to copy messages from one folder to another? For example move messages from TEST Folder to Inbox via script?

Glen said...

yes you can use Moverecord with Exoledb or using CDO 1.2 and use Copyto.