<SCRIPT RunAt=Server Language=VBScript>

'THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT 
'WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, 
'INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
'OF MERCHANTABILITY AND/OR FITNESS FOR A  PARTICULAR 
'PURPOSE

'------------------------------------------------------------------------------
'FILE DESCRIPTION: Sample Exchange Server Event Script 
'
'File Name: CounterV.txt
' 
' Copyright (c) Microsoft Corporation 1993-1997. All rights reserved.
'------------------------------------------------------------------------------

Option Explicit 

'------------------------------------------------------------------------------
' Localized strings
'------------------------------------------------------------------------------
' Put all localizable strings and constants here

'------------------------------------------------------------------------------
' Global Variables
'------------------------------------------------------------------------------

Dim g_bstrDebug    'Debug String
Dim g_CdoPR_CONTENT_COUNT
g_CdoPR_CONTENT_COUNT = &H36020003        'Message Count in Folder

'------------------------------------------------------------------------------
' Event Handlers
'------------------------------------------------------------------------------

' DESCRIPTION: This event is fired when a new message is added to the folder
Public Sub Folder_OnMessageCreated

    Call DebugAppend(vbCrLf&"COUNTERV - Folder_OnMessageCreated",False)
	Call SendResponseMessage("A New Message Was Created")    
	Script.Response = g_bstrDebug

End Sub

' DESCRIPTION: This event is fired when a message in the folder is changed
Public Sub MessageOnChange

    Call DebugAppend(vbCrLf&"COUNTERV - MessageOnChange",False)	
    Call SendResponseMessage("A Message Has Changed")
    Script.Response = g_bstrDebug
	
End Sub

' DESCRIPTION: This event is fired when a message is deleted from the folder
Public Sub Folder_OnMessageDeleted
	
    Script.Response = vbCrLf & "COUNTERV - Folder_OnMessageDeleted"
	
End Sub

' DESCRIPTION: This event is fired when the timer on the folder expires
Public Sub Folder_OnTimer

	Script.Response = vbCrLf & "COUNTERV - Folder_OnTimer"

End Sub

'-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
'                  PRIVATE FUNCTIONS/SUBS
'-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

'------------------------------------------------------------------------------
'   Name: SendResponseMessage
'   Area: App
'   Desc: Get CDO objects and Send Message
'   Parm: None
'------------------------------------------------------------------------------

' DESCRIPTION: Get the details of the event that fired
Private Sub SendResponseMessage(bstrSubject)

    On Error Resume Next

    Dim CDOSession    'Session Object
	Dim oMsg          'Message Object
	Dim oFolder       'Current Folder Object
    Dim oFolderOutbox 'Outbox Folder Object
    Dim oMsgTarget    'Target Msg Object
    Dim oRec          'Recipients Object    
    Dim iMsgCount
       		
	Set CDOSession = EventDetails.Session
	Call DebugAppend("EventDetails.Session",True)
  
    Set oFolderOutbox = CDOSession.Outbox
    Call DebugAppend("CDOSession.Outbox",True)

	Set oFolder = CDOSession.GetFolder(EventDetails.FolderID, Null )
	Call DebugAppend("CDOSession.GetFolder",True)
	
    Set oMsgTarget = CDOSession.GetMessage( EventDetails.MessageID, Null )
	Call DebugAppend("CDOSession.GetMessage",True)
	
    iMsgCount = oFolder.Fields(g_CdoPR_CONTENT_COUNT)
    Call DebugAppend("oFolder.Fields(PR_CONTENT_COUNT)",True)

    'Create Message In Outbox
	Set oMsg = oFolderOutbox.Messages.Add(cstr(bstrSubject), "There are now " & iMsgCount & " messages in the folder")
	Set oRec = oMsg.Recipients
    oRec.Add "", "", 1, oMsgTarget.Sender.ID
	oRec.Resolve(False)
	If oRec.Resolved = True Then
	   oMsg.Send  
       If err.number <> 0 then
           Call DebugAppend("omsg.send",True)
       
       Else
           Call DebugAppend("Message Sent",False)
       End If

    Else
       Call DebugAppend("Recipient.Resolve",True)   
    End If

    'Trap any errors in message creation/sending.
    Call DebugAppend("Error Occured in SendResponseMessage Sub",True)

End Sub


'------------------------------------------------------------------------------
'   Name: DebugAppend
'   Area: Debug
'   Desc: Simple Debugging Function
'   Parm: String Text, Bool ErrorFlag
'------------------------------------------------------------------------------

Private Sub DebugAppend(bstrParm,boolErrChkFlag)

   if boolErrChkFlag = True then
       if err.number <> 0 then
           g_bstrDebug = g_bstrDebug & bstrParm & " Failed: " & cstr(err.number) & err.description & vbCrLf
           err.clear
       end if
   
   else
       g_bstrDebug = g_bstrDebug & bstrParm & vbCrLf
   end if

End Sub


</SCRIPT>
