Logo Twistround LTD  

Frequently Asked Questions
How to load a mail to the CDO Mail Object from file.


There are times when you may want to load a message from file, in order to manipulate or read properties from it.

There are many examples on MSDN of how to do this, but none seem to work when used from a VBScript.

Listed here is a simple function that loads an email, this function can for exampe be used in a POPREP event script
.

Sample code to load email from file to CDO.Message object:

' ++ ' Function LoadMessageFromFile(ByVal Path, ByRef Msg)
' Load an email into a CDO Message object.
' --

Function LoadMessageFromFile(ByVal Path, ByRef Msg)
  LoadMessageFromFile = False
  Set Msg = Nothing

  Dim Stm, iMsg, iDsrc

  Set Stm = CreateObject("ADODB.Stream")
  Stm.Open

  Call Stm.LoadFromFile(Path)

  Set iMsg = CreateObject("CDO.Message")

  Set iDsrc = iMsg.DataSource

  Call iDsrc.OpenObject(Stm, "_Stream")

  Set Msg = iMsg
 
  LoadMessageFromFile = True

End Function