Adding Attachments

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.

The IMessage.AddAttachment method adds an attachment to a message. The AddAttachment method accepts a file, FTP, HTTP, or HTTPS URL parameter that identifies the location of the attachment. The method returns a reference to the newly added BodyPart object that houses the attachment so that it can be further manipulated, if necessary.

The following example shows how to add a Graphics Interchange Format (GIF) graphic and a Microsoft® Word file as attachments to a message.

Visual Basic

' Reference to Microsoft ActiveX Data Objects 2.5 Library
' Reference to Microsoft CDO for Exchange 2000 Server Library
' ..
Dim iMsg As New CDO.Message
' configure message here if necessary
With iMsg
   .To = "someone@example.com"
   .From = "another@example.com"
   .Newsgroups = "comp.example.newsgroup1"
   .Subject = "Agenda for staff meeting"
   .TextBody = "See attached docs for more info."
   .AddAttachment "http://example.example.com/picture.gif"
   .AddAttachment "file://d:/temp/test.doc"
   .AddAttachment "C:\files\another.doc"
   ' finish and send
End With

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));

/*
** configure message here if necessary
*/


iMsg->To         = "someone@example.com";
iMsg->From       = "another@example.com";
iMsg->Newsgroups = "comp.example.newsgroup1";
iMsg->Subject    = "Agenda for staff meeting";
iMsg->TextBody   = "See attached docs for more info.";

try {
  iMsg->AddAttachment("http://example.example.com/picture.gif","","");
  iMsg->AddAttachment("file://d:/temp/test.doc","","");
  iMsg->AddAttachment("C:\files\another.doc","","");
}
catch(_com_error err) {
  // handle exception
}
// finish and send

VBScript

Dim iMsg
Set iMsg = CreateObject("CDO.Message")
' configure message here if necessary
With iMsg
   .To   = "someone@example.com"
   .From = "another@example.com"
   .Newsgroups = "comp.example.newsgroup1"
   .Subject  = "Agenda for staff meeting"
   .TextBody = "See attached docs for more info."
   .AddAttachment "http://example.example.com/picture.gif"
   .AddAttachment "file://d:/temp/test.doc"
   .AddAttachment "C:\files\another.doc"
   ' ..
End With

Note  If you attach a Web page using the AddAttachment method, you attach only the file specified in the URL. If the page contains image links or other content, they are not included. To attach a full Web page with embedded graphics, use the IMessage.CreateMHTMLBody method. For more information about attaching Web pages, see Creating MHTML-Formatted Messages.

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.