Using WebCheck with CDO
Article from Windows 2000 Magazine's IIS Administrator
In "Setting Up WebCheck" (December 1999), I explored the WebCheck utility from the Microsoft Windows Internet Information Server Resource Kit. This utility can monitor your Web site and report when it or other parts of your server aren't working. However, WebCheck works with the mail component on Microsoft Site Server or the Microsoft Commercial Internet System (MCIS). Therefore, if you haven't installed Site Server or MCIS, you can't use the email feature as WebCheck currently implements it. To address this situation, I modified WebCheck to use Collaboration Data Objects for Windows NT Server (CDONTS) instead of the mail component. (CDONTS installs with various Microsoft software products, and you can find it on most systems. You can also download the latest version at http://www.microsoft.com/exchange/downloads/cdo.htm.)
To make this modification, you have to have installed CDONTS on your system. (You need CDONTS to register the CDONTS component and to test the application.) When WebCheck with CDO is working, you can compile it and move it to other servers. You also need Visual Basic (VB) 6.0 to modify and recompile webcheck.vbp. You can use any version of VB 6.0.
On This Page
To begin the modification, open webcheck.vbp in VB by either double-clicking webcheck.vbp in Windows Explorer or starting VB and choosing File, Open, webcheck.vbp. When webcheck.vbp is open in VB, you can begin to modify it. First, create a project reference to the CDONTS library (i.e., a reference in webcheck.vbp to cdonts.dll). To create the reference,
Choose Project, Project References.
Select the Microsoft CDO for NTS 1.2 Library check box, which you see in Screen 1. (I use CDONTS 1.2, but CDONTS 1.21 is now available at the URL shown above.)
Screen 1 Creating a reference to the CDONTS library
Click OK to place the reference to CDONTS in the project .vbp file.
At this point, you have linked CDONTS into the WebCheck application.
Now, to use CDONTS, you have to modify the CreateMail procedure that executes WebCheck's mail function. First, find the procedure CreateMail in main.bas. (You can search for procedures by pressing Ctrl+F to display the Find dialog box.) The original code for CreateMail appears in Listing 1.
LISTING 1: Original CreateMail Code
Sub CreateMail(sMailFrom As String, g_sPageMailTo As String, _sMessageSubject As String, sMessageText As String) Select Case g_iMailToUse Case 1 ' Personalization SMTP Call CreateSMTPMail(sMailFrom, g_sPageMailTo, sMessageSubject, sMessageText) Case 2 ' IIS SMTP Call CreateActiveMail(sMailFrom, g_sPageMailTo, sMessageSubject, sMessageText) End Select End Sub
The Select Case statement in the original code checks the value in the g_iMailToUse variable, which WebCheck set earlier by reading from the webcheck.ini file. Then, use either Site Server or MCIS to execute the proper case statement to send email. You can modify the CreateMail procedure one of two ways.
To replace the code in CreateMail with your CDONTS code,
Copy the code from CreateSMTPMail and paste it into CreateMail.
Remove the existing code from the CreateMail procedure. (This step leaves the procedure block empty.)
Now you can begin to modify the new code in CreateMail. First, change the error-handler label by changing two lines of code. The first line starts with On Error. The line ends with the error-handler label the code branches to if an error occurs. Change this label to ErrorCreateMail to properly reflect the name of the procedure. Then, change the label itself, which is the line ending in a colon (:). The new label is
Next, comment out the two lines that use SMTP to do all the work by placing a single quotation mark (') at the beginning of each line:
' Set smMail = CreateObject("mps.sendmail") ' iMailSent = smMail.sendMail(sMailFrom, g_sPageMailTo, sMessageSubject,
Commenting out the code takes it out of execution but leaves it in the application. Because you're going to replace this code with CDONTS, the new version of WebCheck won't need it.
Next, add a new CreateObject statement to create an instance of CDONTS.NewMail. This code creates an instance of the NewMail object in the CDONTS component. I placed this statement early in the procedure, but it can go anywhere as long as it occurs before the point at which WebCheck uses any of the NewMail objects. The statement looks like this:
Set oMail = CreateObject("CDONTS.NewMail")
Next, modify the code to use the NewMail object. Create a comment block to include the new code so that you can see where your changes are. Now you can begin to use the various properties and methods from the NewMail object to implement the mail features. The values you feed to the properties are pulled from the parameters passed to CreateMail when it executes. Set the from address using this statement:
oMail.From = sMailFrom
Set the to address using this statement:
oMail.To = g_sPageMailTo
Set the subject using this statement:
oMail.Subject = sMessageSubject
Set the message body using this statement:
oMail.Body = sMessageText
When you have set the message properties, you can use the Send method to send the email message:
Finally, set the object variable that represents the CDONTS.NewMail object to Nothing, which releases the object and lets NT 4.0 destroy it, freeing its resources:
Set oMail = Nothing
That completes the changes to the code. Now save the file. (For the complete code, see Listing 2.)
LISTING 2 CreateMail Code Modified to Use CDONTS
Sub CreateMail(sMailFrom As String, g_sPageMailTo As String, _ sMessageSubject As String, sMessageText As String) Dim iMailSent As Integer Dim smMail As Object Dim oMail As CDONTS.NewMail Set oMail = CreateObject("CDONTS.NewMail") On Error GoTo ErrorCreateMail Debug.Print ("g_sPageMailTo=" & g_sPageMailTo) Debug.Print ("sMailFrom=" & sMailFrom) Debug.Print ("sMessageSubject=" & sMessageSubject) Debug.Print ("sMessageText=" & sMessageText) Call MsgBox("sMessageText=" & sMessageText, vbYesNo, "Context") ' *** new CDONTS code *** oMail.From = sMailFrom oMail.To = g_sPageMailTo oMail.Subject = sMessageSubject oMail.Body = sMessageText oMail.Send Set oMail = Nothing ' *** end new CDONTS code *** ' Set smMail = CreateObject("mps.sendmail") ' iMailSent = smMail.sendMail(sMailFrom, g_sPageMailTo, ' sMessageSubject, sMessageText) Call DisplayError("Sent Mail to:" & g_sPageMailTo, 4) Exit Sub ErrorCreateMail: Call DisplayError("Error Creating the SendMail Object", 1) End Sub
You can step through the execution of WebCheck by pressing F5 and running the execution in the VB environment. You can also use the single-step commands on the Debug menu to step through the code one line at a time. If any errors occur, VB stops the application in Debug mode, letting you use the Debug menu commands to inspect your code. (I will cover more about using VB's Debug mode in a future article.)
Compiling the File
The last step in making the change is to compile the file. Select Make webcheck.exe from the File menu, then select the directory in which to store the executable. Make sure you don't overwrite the existing .exe file until WebCheck is working correctly. Click OK to start the compilation process. If you avoided errors in making your changes, the compilation will finish as usual, and you'll have a new webcheck.exe with your changes.
Testing the Compilation
When you've compiled WebCheck, you can use the information in my December 1999 article to test it. Test WebCheck on a test server on which you can run WebCheck with the Web server running, then shut down the Web server and run WebCheck again. You'll see email to the account you entered when the Web server wasn't running. You can also test WebCheck by pointing it at an existing page, running WebCheck, then renaming the page and running WebCheck again.
Setting Up Your Server
To deploy WebCheck, copy it to a server on which you've installed CDONTS. You need the VB runtime on the server on which you plan to run WebCheck. (If you have VB or another application written in VB installed on the server, you should have the runtime.) You can execute WebCheck by double-clicking webcheck.exe or by using a scheduler service to automate it.
You can also package the new webcheck.exe file in an install package, which will install the runtime for you along with the new executable. Visual Studio (VS) includes the Visual Studio Installer, which you can use to create the install package based on the new Windows Installer. Screen 2 shows the Visual Studio Installer. You can also repackage WebCheck with other tools such as the Visual Basic Packaging and Deployment Wizard.
Screen 2 Creating an install package in Visual Studio Installer
Using Other NewMail Properties
You can use several other methods and properties within the NewMail object. For example, you can create a copy list for any mail by using the Cc or Bcc properties. Cc adds the name or names to a standard copy list; any recipients can see the other names on the list. The Bcc property adds the names to a blind copy list, hiding them from any other recipients. You use both these properties just like you use the To property. To add names to the copy list, use this syntax:
oMail.CC = "firstname.lastname@example.org"
You can change the importance of a message by using the Importance property. For example, to set the importance as High, use this syntax:
oMail.Importance = CdoHigh
The CdoHigh entry in the above statement refers to a constant. The reference you created to the CDONTS library supplies this constant. You can see the values for the Importance constant in Table 1.
Table 1 Values for the Importance Constant
Normal importance (default)
Extending WebCheck's Functionality
You can customize the CreateMail function by using any or all of the features discussed in this article. When you've wired in CDONTS, the other options are available. Because WebCheck can already check the status of NT services and Web applications, you can modify it to extend this functionality. You could easily have WebCheck test all the servers in your network for particular services, Web applications, and file services. In a future issue, I'll explore using File System Object (FSO) with Active Server Pages (ASP) and VB and discuss where you can use FSO with applications like WebCheck.
About the Author
Ken Spencer is a contributing editor for Windows 2000 Magazine and SQL Server Magazine. He writes for the weekly email newsletter SQL Server UPDATE (http://www.winntmag.com/update). He is vice president of 32X Tech and has authored several books. You can reach him at email@example.com.
The above article is courtesy of Windows 2000 Magazine's IIS Administrator.