Manually Adding Attachments

Manually Adding Attachments

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

You can manually add attachments directly to the IMessage.Attachments or IBodyPart.BodyParts collection that is 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.

To manually add an attachment using the IMessage.Attachments collection

  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 Multipurpose Internet Mail Extensions (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@example.com"
   .From = "another@example.com"
   .Newsgroups = "comp.example.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:\program files\common files\microsoft shared\cdo\cdoex.dll" no_namespace
// ...
IMessagePtr iMsg(__uuidof(Message));
IBodyPartPtr iBp;
_StreamPtr Stm;
FieldsPtr Flds;

iMsg->To         = "someone@example.com";
iMsg->From       = "another@example.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@example.com"
   .From       = "another@example.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. Collaboration Data Objects (CDO) automatically sets the MIME Content-Type and Content-Transfer-Encoding values for some attachments, but for some attachment types you need to set these values explicitly.

If you want to have the message sent with attachments formatted and encoded with Unix-to-Unix encode (UUENCODE), set the IMessage.MIMEFormatted property to False before sending the message.

In many cases, you might need to specify credentials to access a resource on the network. Add this information to the Message object's associated Configuration object. See Configuring the Message Object for more information.

Send us your feedback about the Microsoft Exchange Server 2003 SDK.

Build: June 2007 (2007.618.1)

© 2003-2006 Microsoft Corporation. All rights reserved. Terms of use.