Platform SDK: Exchange 2000 Server

FileName Property

[This is preliminary documentation and subject to change.]

Returns the name of the file containing the message on the file system.

[Visual Basic]
Property FileName(Var as Variant) as String
read-only
[C++]
HRESULT get_FileName(VARIANT var, BSTR* Filename)
[IDL]
HRESULT [propget] FileName([in] VARIANT var, [out,retval] BSTR* Filename);
var
The ordinal index or IMessage interface of the object for which you are requesting the file name.
Filename
On return, the full path to the file from which the specified Message object was created. The Filename is the logical result when getting the property in languages such as Visual Basic.

Remarks

You can identify which message for which you want the file name using the first argument to the property. You can specify either the ordinal index of the Message object, or pass the IMessage object reference itself.

One common use of the FileName property is when you want to modify the contents of the messages stored in a particular directory and save the changes back into the file from which the Message object was derived. When you retrieve a Messages collection, each Message object in the collection was created using the serialized message stream stored in a file with the .eml extension. Unlike messages stored in the Microsoft Exchange store, you cannot use the IDataSource interface on the Message object to bind to the file, call IDataSource.Save, and thereby save the modified message contents back to the file. In such cases, you must use an ADO Stream object, or some other means, such as a Scripting.FileSystemObject object to save the new message contents back to the file system. You can use the FileName property to get the file name, and then overwrite the file with the new contents of the message. You can use the IMessage.GetStream method to return the contents of the Message object in serialized format within an ADO Stream object. The _Stream interface defines the _Stream.SaveToFile method that can be used to save the stream to disk. The example below demonstrates how to do this.

Example

This example appends some text to the bottom of each text/plain body part of each message in the SMTP drop directory, and then saves the modified message back into the file from which the Message object was derived.

[Visual Basic]
Dim iDropDir as New CDO.DropDirectory
Dim iMsgs as CDO.IMessages
Dim iMsg as CDO.Message
Dim iStream as ADODB.Stream
Dim fileName as String
Dim TxtBody as String

Set iMsgs = iDropDir .GetMessages
For Each iMsg in iMsgs
  TxtBody = ""
  fileName = ""
  fileName = iMsgs.FileName(iMsg)
  TxtBody = iMsg.TextBody
  TxtBody = TxtBody & vbCrLf & vbCrLf & "Here is some appended text"
  iMsg.TextBody = TxtBody
  Set iStream = iMsg.GetStream
  iStream.SaveToFile fileName, adSaveCreateOverWrite
Next iMsg
[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>

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;
      IMessage* pMsg = NULL;
      var.pdispVal->QueryInterface(__uuidof(IMessage),(void**)&pMsg);
      var.pdispVal->Release();
      VariantClear(&var);

      // get file name
      BSTR szFilename;
      VARIANT varIntf;
      VariantInit(&varIntf);
      V_VT(&varIntf) = VT_DISPATCH;
      varIntf.pdispVal = pMsg;
      varIntf.pdispVal->AddRef();
      pMsgs->get_Filename(varIntf,&szFilename);

      // get text body of message
      BSTR szTxtBody;
      pMsg->get_TextBody(&szTxtBody);
      _bstr_t tempBody(szTxtBody);
      tempBody += "\r\n\r\nThis is some appended text";
      pMsg->put_TextBody(BSTR(tempBody);
      SysFreeString(szTxtBody);

      _Stream* pStrm = NULL;
      pMsg->GetStream(&pStrm);
      // write changes back to file
      pStrm->SaveToFile(szFilename,adSaveCreateOverWrite);
      pStrm->Release();
      SysFreeString(szFilename);
      pMsg->Release();
    }
  CoUninitialize();
}