Compartir a través de


Message CoClass

Topic Last Modified: 2006-06-29

Defines an object that represents a message.

CLSID

CD000001-8B95-11D1-82DB-00C04FB1625D

ProgID

CDO.Message

Type Library

Microsoft CDO for Windows 2000 Library

Microsoft CDO for Exchange 2000 Library

Inproc Server

CDOSYS.DLL, CDOEX.DLL

Threading Model

Both

Implemented Interfaces

IBodyPart Interface

IDataSource Interface

IMessage Interface

OLE DB Row Access

Supported Bindings

The following table lists IDataSource interface bindings.

Method Target Argument Content Class

Open

Exchange store item URL

urn:content-classes:calendarmessage

OpenObject

IRow _Record IStream _Stream IBodyPart

urn:content-classes:calendarmessage

SaveTo

Exchange store item URL

N/A

SaveToContainer

Exchange store folder URL

N/A

SaveToObject

IRow _Record IStream _Stream IBodyPart

N/A

Remarks

An instance of the Message Component Object Model (COM) class represents a message. The primary and default interface on Message objects is the IMessage Interface. You can use this interface to access the messaging-specific functionality of the object, including addressing the message, adding content, sending, posting, or responding to the message.

Messages contain a set of message headers that contain information, such as to whom the message is to be sent, to whom carbon copies are to be sent, and so forth. The most common message header fields are present as properties on the IMessage interface, and all of these fields are accessible using the Fields collection of the Message object. All fields reside in some namespace, such as urn:schemas:mailheader, for the purpose of rendering them unique. In other words, when you are accessing the field in the Fields collection, you must use the fully qualified name of the field, such as urn:schemas:mailheader:subject for the message subject or urn:schemas:httpmail:textdescription for the text/plain part of the message.

The IMessage interface provides a set of top-level methods and properties designed to make creating the most common message content straightforward and intuitive. Such methods include CreateMHTMLBody, AddBodyPart, and AddAttachment. Properties include TextBody and HTMLBody.

For Multipurpose Internet Mail Extensions (MIME)-formatted messages, the Message object acts as the root body part of the MIME body part hierarchy. This functionality is provided through the exposed IBodyPart interface on the object. The IMessage interface provides the BodyPart property that returns this interface on the object. You can use BodyPart, the Getinterface method, or standard mechanisms such as Set (in Microsoft® Visual Basic®) and Queryinterface (in C++) to navigate to this interface.

To facilitate the easy transfer of message data to and from other objects, the Message object provides an implementation of the IDataSource interface. Using methods and properties on this interface, you can access embedded messages in other messages, and embed messages in other messages. The IMessage interface provides the DataSource property to allow navigating to the IDataSource interface on the object.

Collaboration Data Objects (CDO) messaging has additional capabilities with Microsoft Exchange Server 2007 . To send messages using Exchange (through the Exchange mail submission URI), you set the https://schemas.microsoft.com/cdo/configuration/sendusing field to cdoSendUsingExchange in the Configuration object of the Message object. Similarly, you set the https://schemas.microsoft.com/cdo/configuration/postusing field to cdoPostUsingExchange to post using Exchange.

When you send or post messages using Exchange, Collaboration Data Objects (CDO) needs a connection to the user's Exchange mailbox. You can specify the URL to the user's mailbox folder using the https://schemas.microsoft.com/cdo/configuration/mailboxurl property in the Configuration object of the Message object. If you do not specify the URL, CDO searches the Active Directory® directory service for the necessary information to construct this URL using the user principal name found in the https://schemas.microsoft.com/cdo/configuration/sendusername or https://schemas.microsoft.com/cdo/configuration/postusername configuration fields.

The data source of the Message object is changed when you send or post messages using Exchange. If the object was previously bound to an item in the Exchange store, that binding is lost. You can override this behavior by specifying an open Connection object in the https://schemas.microsoft.com/cdo/configuration/activeconnection configuration field. If this object reference is present in the Configuration Fields collection, this Connection object is used to send or post the message. If the Connection object is not open and bound to the user's mailbox before the message is sent, CDO generates an error. If you want to send the message within a transaction, you must start and commit it manually using the referenced Connection object.

You can post directly to a public folder by specifying the URL of the folder in the https://schemas.microsoft.com/exchange/hardlinklist property. Although this field is multivalued, you can only specify one folder URL.

Note

There is a difference between saving a message to a folder and posting to a folder. Posting sets property values such as the post date and performs validations. You can examine the X-Unsent field to determine if a particular message was posted.

Examples


' Reference to Microsoft CDO for Exchange 2000 Library
' Reference to ActiveX Data Objects 2.5 Library
' Reference to Active DS Type Library

Sub Main()

    Dim Info As New ADSystemInfo
    Dim InfoNT As New WinNTSystemInfo
    Dim iPer As New CDO.Person
    Dim sTo As String
    Dim sFrom As String
    Dim sSubject As String
    Dim sText As String

    sTo = InfoNT.UserName & "@" & Info.DomainDNSName
    sFrom = sTo
    sSubject = "Subject"
    sText = "Text of message."

    iPer.DataSource.Open "LDAP://" & Info.UserName

    Dim iMbx As CDO.IMailbox
    Set iMbx = iPer

    SendMessageUsingExchange sTo, sFrom, sSubject, sText, iMbx.BaseFolder

End Sub

Sub SendMessageUsingExchange(sTo As String, sFrom As String, sSubject As String, sText As String, sMailboxURL As String)

    Dim iMsg As New CDO.Message
    Dim iBp As CDO.IBodyPart
    Dim Flds As ADODB.Fields
    Dim Conn As New ADODB.Connection
    Dim Stm As ADODB.Stream

    Conn.Provider = "ExOLEDB.DataSource"
    Conn.Open sMailboxURL

    Dim iConf As New CDO.Configuration
    Set Flds = iConf.Fields

    Flds(cdoSendUsingMethod) = cdoSendUsingExchange
    Flds(cdoMailboxURL) = sMailboxURL
    Flds(cdoActiveConnection) = Conn
    Flds.Update

    With iMsg
     Set .Configuration = iConf
         .To = sTo
         .From = sFrom
         .Subject = sSubject
         .TextBody = sText

      Set iBp = .AddAttachment("c:\wordfile.doc")
      Set Stm = .GetStream
      Stm.SaveToFile "c:\mysavedmessage.eml", adSaveCreateOverWrite
        .Send
    End With

    ' Clean up.
    Conn.Close
    Stm.Close
    Set Conn = Nothing
    Set Stm = Nothing

End Sub

VBScript

<Job id="sendselfmessage">
<reference object="cdo.message"/>
<reference object="adodb.connection"/>
<script language="VBScript">

 Dim Info
 Set Info = CreateObject("ADSystemInfo")
 Dim InfoNT
 Set InfoNT = CreateObject("WinNTSystemInfo")

 sTo = InfoNT.UserName & "@" & Info.DomainDNSName
 sFrom = sTo
 sSubject = "Subject"
 sText = "Text of message."

 Dim iPer
 Set iPer = CreateObject("CDO.Person")
 iPer.DataSource.Open "LDAP://" & Info.UserName

 Dim iMbx
 Set iMbx = iPer.Getinterface("IMailbox")

 SendMessageUsingExchange sTo, sFrom, sSubject, sText, iMbx.BaseFolder


Sub SendMessageUsingExchange( sTo, sFrom, sSubject, sText, sMailboxURL)

    Dim iMsg
    Set iMsg = CreateObject("CDO.Message")
    Dim iBp
    Dim Flds
    Dim Conn
    Dim Stm
    Set Conn = CreateObject("ADODB.Connection")
    Conn.Provider = "ExOLEDB.DataSource"
    Conn.Open sMailboxURL

    Dim iConf
    Set iConf = CreateObject("CDO.Configuration")
    Set Flds = iConf.Fields

    Flds(cdoSendUsingMethod)  = cdoSendUsingExchange
    Flds(cdoMailboxURL)       = sMailboxURL
    Flds(cdoActiveConnection) = Conn
    Flds.Update

    With iMsg
     Set .Configuration = iConf
         .To       = sTo
         .From     = sFrom
         .Subject  = sSubject
         .TextBody = sText

      Set iBp = .AddAttachment("c:\wordfile.doc")
      Set Stm = .GetStream

      ' Note: using adSaveCreateOverWrite may overwrite an existing item of the same name.
      Stm.SaveToFile "c:\mysavedmessage.eml", adSaveCreateOverWrite
        .Send
    End With

    ' Clean up.
    Conn.Close
    Stm.Close
    Set Conn = Nothing
    Set Stm = Nothing

End Sub

</script>
</Job>



 // You must have the following paths in your
 // include path:
 // %CommonProgramFiles%\system\ado
 // %CommonProgramFiles%\microsoft shared\cdo
 //
 // Import the type libraries into the project:

 // #import <msado15.dll> no_namespace raw_interfaces_only
 // #import <cdoex.dll>   no_namespace raw_interfaces_only
#ifndef _CORE_EXAMPLE_HEADERS_INCLUDED
#define _CORE_EXAMPLE_HEADERS_INCLUDED
#import <msado15.dll> no_namespace
#import <cdoex.dll> no_namespace
#include <iostream.h>
#endif

IMessagePtr CreateSampleMessage2() {

   IMessagePtr      pmsg(__uuidof(Message));
   IBodyPartPtr      bp;
   IConfigurationPtr   config(__uuidof(Configuration));
   FieldsPtr      flds;
   FieldPtr      fld;
   _StreamPtr      stm;

   _bstr_t bstrEmpty("");

   pmsg->To      = "\"Some One\" <someone@example.com>, \"Another\" <another@example.com>";
   pmsg->From      = "\"ThirdPerson\" <thirdperson@example.com>, \"Fourth\" <fourth@example.com>";
   pmsg->Sender   = "\"Finally\" <finally@example.com>";
   pmsg->Subject   =   "A really cool message.";

   // Get the current directory so that relative paths
   // can be expanded. This is necessary for AddAttachment.
   LPTSTR buf = NULL;
   DWORD buflen = 0;
   _bstr_t path;
   if( ( buflen = GetCurrentDirectory(buflen,NULL) ) > 0 ) {
      buf = new TCHAR[buflen+1];
      GetCurrentDirectory(buflen+1,buf);
      path = buf;
      delete [] buf;
   }
   else {
      cerr << "Error getting current directory" << endl;;
      throw _com_error(GetLastError());
   }

   try {
      bp = pmsg->AddAttachment(path + _bstr_t("\\..\\misc\\wordfile.doc"),
                           bstrEmpty,
                           bstrEmpty);

      bp->Fields->Item["urn:schemas:mailheader:content-type"]->Value = variant_t("application/msword");
      bp->Fields->Update();
      bp = pmsg->AddAttachment(path + _bstr_t("\\..\\misc\\message2.eml"),
                           bstrEmpty,
                           bstrEmpty);
      bp->Fields->Item["urn:schemas:mailheader:content-type"]->Value = _variant_t("message/rfc822");
      bp->Fields->Update();

      pmsg->CreateMHTMLBody(_bstr_t("http://www.example.com"),cdoSuppressAll,_bstr_t(""),_bstr_t(""));
   }
   catch(_com_error e) {
      cerr << "Error creating sample message!" << endl;
      throw e;
   }

   // Save a copy to the local file system.
   stm = pmsg->GetStream();

   // Note: using adSaveCreateOverWrite may overwrite an existing item of the same name.
   stm->SaveToFile(_bstr_t("savemymessage.eml"),adSaveCreateOverWrite);

   // Clean up.
   stm->Close();

   return pmsg;
}