Platform SDK: CDO for Windows 2000

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:

[Visual Basic]
' 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          = "someone@microsoft.com"
   .From        = "another@microsoft.com"
   .Newsgroups  = "comp.microsoft.newsgroup1"
   .Subject     = "Agenda for staff meeting"
   .TextBody    = "See attached docs for more info."
   .AddAttachment "http://example.microsoft.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 <cdosys.dll> no_namespace
// ...
IMessagePtr iMsg(__uuidof(Message));

/*
** configure message here if necessary
*/

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

try {
  iMsg->AddAttachment("http://example.microsoft.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@microsoft.com"
   .From = "another@microsoft.com"
   .Newsgroups = "comp.microsoft.newsgroup1"
   .Subject  = "Agenda for staff meeting"
   .TextBody = "See attached docs for more info."
   .AddAttachment "http://example.microsoft.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

Creating MHTML Messages

IMessage.CreateMHTMLBody Method

IMessage.HTMLBody Property

IMessage.HTMLBodyPart Property