Platform SDK: CDO for Windows 2000

IMessages Interface

The IMessages interface defines methods and properties that you can use to access a collection of Message objects retrieved by a call to the IDropDirectory.GetMessages method.

IID
CD000025-8B95-11D1-82DB-00C04FB1625D
Extends
IDispatch

Member Summary

Properties

Name Type Description
Count

(Read-only)

[Visual Basic] Long

[C++] long

Returns the number of Message objects contained in the collection.
Item

(Read-only)

[Visual Basic] Message

[C++] IMessage*

Returns an IMessage interface on the Message object specified.
Filename

(Read-only)

[Visual Basic] String

[C++] BSTR

Returns the name of the file in which a particular message is stored, in serialized format, on the file system.
_NewEnum

(Read-only)

[Visual Basic] N/A

[C++] IUnknown*

Returns an IEnumVariant interface on an object that can be used to enumerate the collection. This is automatic in Microsoft® Visual Basic® For Each In constructs.

Methods

Name Description
Delete Deletes the specified Message object and the associated file from which the object was derived.
DeleteAll Deletes all Message objects in the collection and all associated files on the file system.

Remarks

The IDropDirectory.GetMessages method returns a Messages collection object containing messages from a specified file system folder. You can get the total count of Message objects in the collection using the IMessages.Count property and retrieve one of the messages by using the IMessages.Item method, and you can get the name of the file that houses the message on the file system by using the IMessages.Filename property. To enumeration the collection, use the IMessage._NewEnum property. In Visual Basic, this happens automatically when a For Each Obj in Collection statement is used.

Example

This example demonstrates how you can use the DropDirectory object (exposing the IDropDirectory interface) to enumerate the messages in the Simple Mail Transfer Protocol (SMTP) default drop directory and then copy each message addressed to a particular local account into the appropriate account mailbox directory; for example, you can locate the local Administrator account's personal mail directory in c:\maiboxes\administrator directory.

[Visual Basic]
' Reference to Microsoft ActiveX Data Objects 2.5 Library
' Reference to Microsoft CDO for Windows 2000 Library
Dim iDropDir as New CDO.DropDirectory
Dim iMsgs as CDO.IMessages
Dim iMsg as CDO.Message
Dim iStream as ADODB.Stream

Dim strTo as String
Dim ToRecipients as Variant
Dim strEmailName as String
Dim strAccountName as String
Dim strFileName as String
Dim strMailboxDir as String

Set iMsgs = iDropDir.GetMessages

For Each iMsg in iMsgs
  strFileName = iMsgs.FileName(iMsg)

  ' trim to get the short file name 
  ' from the full path
  strFileName = Right(strFileName, Len(strFileName) - InStrRev(strFileName,"\") )

  ' Get the To recipients...and assume they are all local accounts
  strTo = iMsg.To
  ToRecipients = Split(strTo,",")
  Dim j, lpos, rpos, posdiff
  ' loop through recipients and get account names for each.
  ' We get the account name by getting the string
  ' between "<" and "@" in the address.
  For j = LBound(ToRecipients) to UBound(ToRecipients)
    strEmailName = ToRecipients(j)
    lpos = InStr(strEmailName,"<")
    rpos = InStr(strEmailName,"@")
    posdiff = rpos - lpos - 1
    strAccountName = Mid(strEmailName,lpos + 1, posdiff)
    
    ' For the purposes of this example,
    ' each account's mailbox directory resides in the
    ' directory c:\mailboxes. For user Joe, their account
    ' directory would be c:\mailboxes\joe
    strMailboxDir = "c:\mailboxes\" & strAccountName
    ' Get the message stream 
    Set iStream = iMsg.GetStream
    
    ' write the stream to the user's mailbox directory
    ' the file name is the same as the one in the drop directory
    iStream.SaveToFile strMailboxDir & "\" & strFileName
    iStream.Close
    Set iStream = Nothing
  Next j
Next iMsg

' once we're done, delete the picked up messages
' this deletes the files from the file system as well.
iMsgs.DeleteAll

Set iMsgs = Nothing
Set iDropDir = Nothing
[C++,IDL]
#import "d:\program files\common files\system\ado\msado15.dll" no_namespace raw_interfaces_only
#import <cdosys.dll> no_namespace raw_interfaces_only
#include <iostream.h>
#include <assert.h>
#include <wchar.h>

void main(int argc, char* argv[])
{
  CoInitialize(NULL);

  IDropDirectory* pDropDir = NULL;
  IMessages*      pMsgs    = NULL;
  IUnknown*       pUnk     = NULL;
  IEnumVARIANT*   pEnum    = NULL;

  CoCreateInstance(
                  __uuidof(DropDirectory),
                  NULL,
                  CLSCTX_SERVER,
                  __uuidof(IDropDirectory),
                  (void**)&pDropDir));

  pDropDir->GetMessages(L"",&pMsgs);
  long count = 0;
  pMsgs->get_Count(&count);
  cout << count << endl;

   pMsgs->get__NewEnum(&pUnk);
   pUnk->QueryInterface(__uuidof(IEnumVARIANT),(void**)&pEnum);

   ULONG cFetched = 0;
   VARIANT var;
   VariantInit(&var);
   while (1) {
      cFetched = 0;
      pEnum->Next(1,&var,&cFetched);
      cout << "fetched: " << cFetched << endl;
      if(cFetched == 0)
         break;
      assert(var.vt == VT_DISPATCH);
      IMessage* pMsg = NULL;

       var.pdispVal->QueryInterface(__uuidof(IMessage),(void**)&pMsg);
       var.pdispVal->Release();
       VariantClear(&var);
       VARIANT varIntf;
       VariantInit(&varIntf);
       V_VT(&varIntf) = VT_DISPATCH;
       varIntf.pdispVal = pMsg;
       varIntf.pdispVal->AddRef();
       BSTR szFilename;
       pMsgs->get_FileName(varIntf,&szFilename);
       VariantClear(&varIntf);

       cout << _bstr_t(szFilename) << endl;
       wchar_t* pwchar;
       pwchar = wcsrchr(szFilename,'\\');
       _bstr_t mailfilename(pwchar+1);
       BSTR to;
       pMsg->get_To(&to);
       wchar_t* pWChar = to;

       wchar_t szbuffer[100];
       wchar_t lt = '<';
       wchar_t amp = '@';
       wchar_t* pFirst;
       unsigned int curPos = 0;
       _bstr_t mailboxesroot = "c:\\mailboxes\\";

       for(unsigned int k = 0;k<wcslen(pWChar);k++){
          if(pWChar[k] == lt) {
          pFirst = &pWChar[k];
          curPos = k;
       }
       if(pWChar[k] == amp) {
          cout << "amp" << endl;
          wcsncpy(szbuffer,pFirst+1,k-curPos-1); 
          buffer[k-curPos-1] = (wchar_t)'\0';
         _bstr_t filen = mailboxesroot + _bstr_t(buffer) + "\\" + _bstr_t(mailfilename);
         _Stream* pStrm = NULL;
         pMsg->GetStream(&pStrm);
         pStrm->SaveToFile(filen,adSaveCreateOverWrite);
         pStrm->Release();
     }
   }
   pMsg->Release();

  }

  CoUninitialize();
}

See Also

IDropDirectory.GetMessages