Loading in-memory MIME data into a CDO IMessage object.

Posted by

I've been working with CDO (Collaborative Data Objects) from C# recently, and while most of it works fairly well, one thing I was having trouble with was loading an in-memory byte array of MIME-encoded data into a CDO.IMessage object. There seemed to be no simple example on the internet anywhere.

So anyway, after much anguish, I finally found what I was looking for. Basically, you need to load the binary data into an ADODB.Stream object, and use the CDO.IMessage.DataSource.OpenObject method. Here's a sample of the code I came up with:


public CDO.IMessage GetCdoEmail(byte[] mime)
{
    CDO.IMessage email = new CDO.MessageClass();

    // We need to save the mime into an ADODB.Stream object
    ADODB.Stream stream = new ADODB.StreamClass();
    stream.Type = ADODB.StreamTypeEnum.adTypeBinary;
    stream.Open(Missing.Value, ADODB.ConnectModeEnum.adModeUnknown,
      ADODB.StreamOpenOptionsEnum.adOpenStreamUnspecified,
      null, null);
    stream.Write(mime);
    stream.Position = 0;

    // Then use the CDO.IMessage.DataSource's OpenObject
    // method to load the message from that stream.
    email.DataSource.OpenObject(stream, "_Stream");

    return email;
}
blog comments powered by Disqus