Platform SDK: Exchange 2000 Server

Manually Adding Attachments

[This is preliminary documentation and subject to change.]

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 URL addressable. When manually adding an attachment, use the IMessage.Attachments collection as described in the following procedure:

  1. Create a new BodyPart object in the Attachments collection using IBodyParts.Add. (The Attachments property on the object returns a BodyParts collection object reference.)
  2. Set appropriate MIME header fields for the body part.
  3. Obtain the content stream for the attachment using IBodyPart.GetDecodedContentStream and write the content to it
  4. Call _Stream.Flush to commit written data to the body part.
[Visual Basic]
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 "c:\exchsrvr\cdoex.dll" no_namespace
// ...
IMessagePtr iMsg(__uuidof(Message));
IBodyPartPtr iBp;
_StreamPtr Stm;
FieldsPtr Flds;

iMsg->To         = "someone@microsoft.com";
iMsg->From       = "another@microsoft.com";
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]
Dim iMsg
Set iMsg = CreateObject("CDO.Message")
Dim iBp
Dim Stm
Dim Flds

With iMsg
   .To         = "someone@microsoft.com"
   .From       = "another@microsoft.com"
   .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

You can add any type of attachment to a message. For MIME-formatted messages, attachments are added as MIME body parts. CDO automatically sets the MIME Content-Type and Content-Transfer-Encoding values 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 Message object's associated Configuration object. See Configuring the Message Object for more information.

See Also

IMessage.CreateMHTMLBody Method

IMessage.Attachments Property

Creating MHTML Messages