Friday, August 13, 2004

Loading a Message in CDO from a text variable

This is something i learned today,

Loading a message from a file is well know and documented here using the ADO stream object loadfromfile method. But what if say instead of the message being stored in a serialized format in a eml file its stored exactly the same way in a SQL database.

The way I found that worked was to first create a new stream object and open it, write the text into the stream using stream.writetext (in this case it was a serialized version of a email exactly the same as a eml file yet stored in a database field). An then you flip the stream type to binary which converts the stream to binary for you and then you can use the normal open message from ado stream method. Here's what it look likes

set stm = createobject("ADODB.Stream")
set msgobj = createobject("CDO.Message")
stm.open
msgstring = rs.fields("messagebody")
stm.type = 2
stm.Charset = "x-ansi"
stm.writetext msgstring,0
stm.Position = 0
stm.type = 1
msgobj.datasource.openobject Stm, "_Stream"


No comments: