GetDecodedContentStream Method

GetDecodedContentStream Method

The GetDecodedContentStream method returns a Microsoft® Active X® Data Objects (ADO) Stream object that contains the content of the body part in decoded format.

Syntax

Function GetDecodedContentStream( ) as ADODB.StreamHRESULT GetDecodedContentStream( _Stream** pVal);

Parameters

  • pVal
    On successful return, a reference to an ADO Stream object.

Remarks

Microsoft Collaboration Data Objects (CDO) uses the current value of the ContentTransferEncoding property and the MIMEFormatted property to determine which encoding mechanism to use to decode or encode the contents of the implementing object. It is the programmer's responsibility to set the appropriate value for the ContentTransferEncoding property.

The Stream object returned contains a read/write copy of the current (decoded) contents of the object implementing IBodyPart. If you modify the contents within the returned Stream object, you must call the Flush method to commit the changes back into the object.

The GetDecodedContentStream method returns an error if it is called on an object that has its Content-Type header field type set to "multipart."

If the Content-Type for the body part is set to some form of text, such as text/html, the returned ADO Stream object has its type (_Stream.Type) automatically set to adTypeText. In all other cases, the type is set to adTypeBinary. With text streams, the _Stream.Charset is automatically set to the character encoding specified by IBodyPart.Charset or urn:schemas:mailheader:content-type with a couple of exceptions: in the case of the character encodings UTF-7, UTF-8, EUC-JP, and ISO-2022-JP, the returned stream has _Stream.Charset set to Unicode. If you wish to load content from a file that has its text content encoded using the UTF-7, UTF-8, EUC-JP, and ISO-2022-JP character encoding formats, you will need to first load the file with a separate ADO Stream object, and then copy the content to the decoded content stream:

Dim iMsg As New CDO.Message
Dim iBp As CDO.IBodyPart
Dim Stm As ADODB.Stream
Dim Stm2 As New ADODB.Stream
With iMsg
  .To      = "example@example.com"
  .From    = "example@example.com"
  .Subject = "hello there"
  .TextBody = "here is my xml file"
End With

' Open the XML file using the 2nd, temporary Stream object
With Stm2
  .Open
  .Type = adTypeText
  .Charset = "UTF-8"
  .LoadFromFile "c:\test.xml"
End With

' Now add the attachment to the message
Set iBp = iMsg.Attachments.Add
With iBp
  .ContentMediaType = "text/xml; charset=""utf-8"""
  .Charset = cdoUTF8
  Set Stm = .GetDecodedContentStream
  ' Copy the XML file contents to the content stream
End With

Stm2.CopyTo Stm
Stm.Flush
' Save the file to check our work
iMsg.GetStream.SaveToFile "c:\test.eml", adSaveCreateOverWrite

To obtain the encoded content, use the GetEncodedContentStream method.

Example

Dim iMsg as New CDO.Message
Dim iBp as CDO.IBodyPart
Set iBp = iMsg.BodyPart.AddBodyPart
Dim Strm as ADODB.Stream
Set Strm = iBp.GetDecodedContentStream
Strm.WriteText "This is some text for the body part"
Strm.Flush
' add more body parts, etc
...
iMsg.Send

See Also

Concepts

GetEncodedContentStream Method