Platform SDK: CDO for Windows 2000

Manually Adding Attachments

You can also manually add attachments directly to the IMessage.Attachments or IBodyPart.BodyParts collection exposed by the Message object. This approach is useful if you have content that is not on the file system or Uniform Resource Locator (URL) addressable. When manually adding the attachment, use the IMessage.Attachments collection as described in the following procedure:

  1. Create a new BodyPart object in the Attachments collection using the IBodyParts.Add method (The Attachments property on the object returns a BodyParts collection object reference.)
  2. Set appropriate Multipurpose Internet Mail Extensions (MIME) header fields for the body part.
  3. Obtain the content stream for the attachment using the IBodyPart.GetDecodedContentStream method and write the content to it.
  4. Call the _Stream.Flush method to commit written data to the body part.
[Visual Basic]
' Reference to Microsoft ActiveX Data Objects 2.5 Library
' Reference to Microsoft CDO for Windows 2000 Library
Dim iMsg as New CDO.Message

Dim iBp as CDO.IBodyPart
Dim Stm as ADODB.Stream
Dim Flds as ADODB.Fields

With iMsg
   .To         = "someone@microsoft.com"
   .From       = "another@microsoft.com"
   .Newsgroups = "comp.microsoft.newsgroup1"
   .Subject    = "Agenda for staff meeting"
   .TextBody   = "Please plan to present your status for the following projects..."
   Set iBp  = .Attachments.Add
   Set Flds = iBp.Fields

   With Flds
      .Item("urn:schemas:mailheader:content-type")              = "text/plain; name=test.txt"
      .Item("urn:schemas:mailheader:content-transfer-encoding") = "quoted-printable"
      .Update
   End With          ' Flds
   Set Flds = Nothing
   Set Stm = iBp.GetDecodedContentStream
   ' Because body part content-type is "text", the returned Stream
   '    is of type adTypeText.  Use WriteText to fill the stream
   Stm.WriteText "Here is the text in the ""attachment"" called text.txt"
   ' commit the changes into the BodyPart object
   Stm.Flush
   Set Stm = Nothing
End With          ' iMsg
[C++,IDL]
#import "c:\program files\common files\system\ado\msado15.dll" no_namespace
#import <cdosys.dll> no_namespace
// ...
IMessagePtr iMsg(__uuidof(Message));
IBodyPartPtr iBp;
_StreamPtr Stm;
FieldsPtr Flds;

iMsg->To         = "someone@microsoft.com";
iMsg->From       = "another@microsoft.com";
iMsg->Newsgroups = "comp.microsoft.newsgroup1";
iMsg->Subject    = "Agenda for staff meeting";
iMsg->TextBody   = "Please plan to present your status for the following projects...";

iBp  = iMsg->Attachments->Add(-1);
Flds = iBp->Fields;
Flds->Item["urn:schemas:mailheader:content-type"]->Value
    =  _variant_t("text/plain;  name=\"test.txt\"");
Flds->Update();

Stm = iBp.GetDecodedContentStream();
  /* 
   **  Because body part content-type is "text", the returned Stream
   **  is of type adTypeText.  Use WriteText to fill the stream
   */
Stm->WriteText("Here is the text in the ""attachment"" called text.txt",adWriteChar);
   ' commit the changes into the BodyPart object
Stm->Flush();

// ...
if(g_Debug)
   cout << iMsg->GetStream()->ReadText(-1);
[VBScript]
Const g_Debug = True
Dim iMsg
Set iMsg = CreateObject("CDO.Message")
Dim iBp
Dim Stm
Dim Flds

With iMsg
   .To         = "someone@microsoft.com"
   .From       = "another@microsoft.com"
   .Newsgroups = "comp.microsoft.newsgroup1"
   .Subject    = "Agenda for staff meeting"
   .TextBody   = "Please plan to present your status for the following projects..."
   Set iBp  = .Attachments.Add
   Set Flds = iBp.Fields

   With Flds
      .Item("urn:schemas:mailheader:content-type")              = "text/plain; name=test.txt"
      .Item("urn:schemas:mailheader:content-transfer-encoding") = "quoted-printable"
      .Update
   End With          ' Flds
   Set Flds = Nothing
   Set Stm = iBp.GetDecodedContentStream
   ' Because body part content-type is "text", the returned Stream
   '    is of type adTypeText.  Use WriteText to fill the stream
   Stm.WriteText "Here is the text in the ""attachment"" called text.txt"
   ' commit the changes into the BodyPart object
   Stm.Flush
   Set Stm = Nothing

   If g_Debug Then
     MsgBox .GetStream.ReadText
   End If
End With          ' iMsg

You can add any type of attachment to a message. For MIME formatted messages, attachments are added as MIME body parts. The MIME Content-Type and Content-Transfer-Encoding values are automatically set for some attachments. Depending on the attachment type, you may need to set these values explicitly.

If you wish to have the message sent with attachments formatted and encoded with Uuencode, set the IMessage.MIMEFormatted property to False before sending the message.

In many cases, you may need to specify credentials to access a resource on the network. In these cases, you will need to add this information to the Configuration object that is associated with the Message object. See Configuring the Message Object for more information.

See Also

IMessage.CreateMHTMLBody Method

IMessage.Attachments Property

Creating MHTML Messages

Configuring the Message Object