The following sample script ships with Microsoft Exchange Server version 5.5 and above. It contains the four basic functions Folder_OnMessageCreated, Message_OnChange, Folder_OnMessageDeleted, and Folder_OnTimer.
Each of these event functions calls two support functions: first GetEventDetails, and then MakeResponseMessage, as described in the following sections.
In addition to error checking, the function GetEventDetails performs the following steps:
In addition to error checking, the function MakeResponseMessage prepares and sends a response message that reports the number of messages in the monitored folder. For this, it uses the CDO response object in the call msgResponse.Send.
<SCRIPT RunAt=Server Language=VBScript>
'---------------------------------------------------------------------
'FILE DESCRIPTION: Sample Exchange Server Event Script for Osmium
'
' Copyright (c) Microsoft Corporation 1993-1997. All rights reserved.
'---------------------------------------------------------------------
Option Explicit
'---------------------------------------------------------------------
' Localized strings
'---------------------------------------------------------------------
' Put all localizable strings and constants here
'---------------------------------------------------------------------
' Global Variables
'---------------------------------------------------------------------
Dim AMSession
Dim fldrOutbox
Dim msgTarget
Dim fldrTarget
'---------------------------------------------------------------------
' Event Handlers
'---------------------------------------------------------------------
' DESCRIPTION: This event is fired when a new message is added to the folder
Public Sub Folder_OnMessageCreated
On Error Resume Next
GetEventDetails
If Err.Number = 0 Then
MakeResponseMessage "A message has been added"
Else
Script.Response = "GetEventDetails Failed"
End If
End Sub
' DESCRIPTION: This event is fired when a message in the folder is changed
Public Sub Message_OnChange
On Error Resume Next
GetEventDetails
If Err.Number = 0 Then
MakeResponseMessage "A message has been changed"
Else
Script.Response = "GetEventDetails Failed"
End If
End Sub
' DESCRIPTION: This event is fired when a message is deleted from the folder
Public Sub Folder_OnMessageDeleted
On Error Resume Next
End Sub
' DESCRIPTION: This event is fired when the timer on the folder expires
Public Sub Folder_OnTimer
On Error Resume Next
End Sub
'---------------------------------------------------------------------
' Support Functions
'---------------------------------------------------------------------
' DESCRIPTION: Get the details of the event that fired
Private Sub GetEventDetails
On Error Resume Next
Dim oStores
Dim Temp
Dim idTargetFolder
Dim idTargetMessage
idTargetFolder = EventDetails.FolderID
idTargetMessage = EventDetails.MessageID
' some of the above may not exist
Err.Clear
Set AMSession = EventDetails.Session
If Err.Number = 0 Then
' We will send a msg, so let's get the Outbox here
Set fldrOutbox = AMSession.Outbox
If Err.Number = 0 Then
Set fldrTarget = AMSession.GetFolder( idTargetFolder, Null )
If Err.Number = 0 Then
Set msgTarget = AMSession.GetMessage( idTargetMessage, Null )
If Not Err.Number = 0 Then
Script.Response = "Session.GetMessage Failed: " & Err.Description
End If
Else
Script.Response = "Session.GetFolder Failed: " & Err.Description
End If
Else
Script.Response = "Session.InfoStores Failed: " & Err.Description
End If
Else
Script.Response = "Outbox.Messages Failed: " & Err.Description
End If
Else
Script.Response = "EventDetails.Session Failed: " & Err.Description
End If
End Sub
' DESCRIPTION: Make a response message
Private Sub MakeResponseMessage(strSubj)
Dim msgResponse
Dim iMsgCount
On Error Resume Next
iMsgCount = fldrTarget.Messages.Count
If Err.Number = 0 Then
Set msgResponse = fldrOutbox.Messages.Add( strSubj, "There are now " & iMsgCount & " messages in the folder")
If Err.Number = 0 Then
msgResponse.Recipients.Add "", "", 1, msgTarget.Sender.ID
If Err.Number = 0 Then
msgResponse.Recipients.Resolve(False)
If msgResponse.Recipients.Resolved = True Then
msgResponse.Send
If Not Err.Number = 0 Then
Script.Response = "Message.Send Failed: " & Err.Description
End If
Else
Script.Response = "Recipients.Resolve Failed: " & Err.Description
End If
Else
Script.Response = "Recipients.Add Failed: " & Err.Description
End If
Else
Script.Response = "Messages.Add Failed: " & Err.Description
End If
Else
Script.Response = "Messages.Count Failed: " & Err.Description
End If
End Sub
</SCRIPT>