Share via


Adding Attachments

Topic Last Modified: 2006-06-12

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.

Example

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_e2k7.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_e2k7.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_e2k7.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.