Adding Attachments

Adding Attachments

The IMessage.AddAttachment method adds an attachment to a message. The AddAttachment method accepts a file, File Transfer Protocol (FTP), Hypertext Transfer Protocol (HTTP), or a Secure Hypertext Transfer Protocol (HTTPS) Uniform Resource Locator (URL) parameter that identifies the attachment's location. The method returns a reference to the newly added BodyPart object that houses the attachment so that it can be further manipulated if needed.

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

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

#import "c:\program files\common files\system\ado\msado15.dll" no_namespace
#import <cdosys.dll> no_namespace
// ...
IMessagePtr iMsg(__uuidof(Message));

/*
** configure message here if necessary
*/

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

try {
  iMsg->AddAttachment("http://server.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
Dim iMsg
Set iMsg = CreateObject("CDO.Message")
' configure message here if necessary
With iMsg
   .To   = "example@example.com"
   .From = "another@example.com"
   .Newsgroups = "area.example.newsgroup1"
   .Subject  = "Agenda for staff meeting"
   .TextBody = "See attached docs for more info."
   .AddAttachment "http://server.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 on attaching Web pages, see Creating MHTML Messages.

See Also

Concepts

Creating MHTML Formatted Messages
CreateMHTMLBody Method
HTMLBody Property
HTMLBodyPart Property