How to Create Scheduled Email Messages Using WSH and CDONTS

ID: Q221495


The information in this article applies to:
  • Microsoft Internet Information Server 4.0
  • Microsoft Internet Information Services version 5.0


SUMMARY

Windows NT Option Pack (NTOP) and Windows 2000 install by default a Simple Mail Transfer Protocol (SMTP) service that can be used for a variety of purposes, usually using Active Server Pages (ASP) for e-mail-based HTML form handling.

The purpose of this article is to describe the process in creating scheduled mail-outs using Windows Script Host (WSH) and the e-mail features of the SMTP service's CDONTS.NewMail object.


MORE INFORMATION

The following example steps you through creating a WSH script to send mail, as well as a text file that you can update for the mail contents and a scheduled task to send the mail.

  1. The first step in setting up any scheduled activity is to ensure that the scheduler service is running."

    1. Click Start on the task bar, click Settings, and then Control Panel.


    2. Double-click the Services applet in Control Panel.


    3. Scroll down to the Task Scheduler service.


    4. Ensure that Status states Running.


    5. Ensure that Startup states Automatic.


    6. Click Close to exit the services applet and close Control Panel.




  2. Create a text file with the following contents and save it as C:\Mailout.txt on your computer:
    
    <p>This is line 1.</p>
    <p>This is line 2.</p> 


  3. Create the WSH script to read the text file you just created. Copy the following code and save it as "C:\mailout.vbs" on your computer. To customize the sender/receiver, change the value of the strFrom and strTo variables:
    
    '--------------------------------------------------------------------
    '
    ' Mailout using CDONTS.NewMail
    '
    '--------------------------------------------------------------------
    
    ' declare all variables
    Option Explicit
    Dim objSendMail
    Dim strTo, strFrom
    Dim strSubject, strBody
    
    ' mail constants (some are for reference)
    Const CdoBodyFormatHTML = 0 ' Body property is HTML
    Const CdoBodyFormatText = 1 ' Body property is plain text (default)
    Const CdoMailFormatMime = 0 ' NewMail object is in MIME format
    Const CdoMailFormatText = 1 ' NewMail object is plain text (default)
    Const CdoLow    = 0         ' Low importance
    Const CdoNormal = 1         ' Normal importance (default)
    Const CdoHigh   = 2         ' High importance
    
    strFrom    = "someone@microsoft.com"  ' change to your email address
    strTo      = "someone@microsoft.com"  ' change to the recipient's address
    strSubject = "Test Message"          ' change to your subject
    
    ' this line calls the ReadFile() function to read the page contents
    strBody = ReadFile("C:\MAILOUT.TXT")
    
    ' this line calls the MakePage() function to format the page as HTML
    strBody = MakePage(strSubject,strBody)
    
    ' the following section creates the mail object and sends the mail
    Set objSendMail = CreateObject("CDONTS.NewMail")
    	objSendMail.From    = strFrom
    	objSendMail.To      = strTo
    	objSendMail.Subject = strSubject & " (" & Date() & ")"
    	objSendMail.Body    = strBody
    
    	objSendMail.BodyFormat = CdoBodyFormatHTML
    	objSendMail.MailFormat = CdoMailFormatMime
    	objSendMail.Importance = CdoNormal
    
    	objSendMail.Send
    Set objSendMail = Nothing
    
    ' this function returns a properly formatted HTML page
    Function MakePage(txtSubject, txtBody)
    	Dim txtTemp
    	txtTemp = "<HTML>" & vbCrLf
    	txtTemp = txtTemp & "<HEAD><TITLE>"
    	txtTemp = txtTemp & txtSubject
    	txtTemp = txtTemp & "</TITLE></HEAD>" & vbCrLf
    	txtTemp = txtTemp & "<BODY>" & vbCrLf
    	txtTemp = txtTemp & "<H2>" & txtSubject & "</H2>" & vbCrLf
    	txtTemp = txtTemp & txtBody & vbCrLf
    	txtTemp = txtTemp & "</BODY>" & vbCrLf
    	txtTemp = txtTemp & "</HTML>"
    	MakePage = txtTemp
    End Function
    
    ' this function opens a file and returns the file's contents
    Function ReadFile(txtFile)
    	Dim txtTemp, objFS, objFL
    	Set objFS = CreateObject("Scripting.FileSystemObject")
    	Set objFL = objFS.OpenTextFile(txtFile)
    	Do While Not objFL.AtEndOfStream
    		txtTemp = txtTemp & objFL.ReadLine
    		txtTemp = txtTemp & vbCrLf
    	Loop	
    	objFL.Close
    	Set objFS = Nothing
    	ReadFile = txtTemp
    End Function 


  4. Create a task to schedule the mail-out to be sent. Open a command session and enter something similar to the following examples:

    1. The following task will run once at 9:00pm.
      AT 9:00pm Cscript.exe C:\Mailout.vbs 


    2. This task will run every Monday at 6:00am.
      AT 6:00am /every:M Cscript.exe C:\Mailout.vbs 


    3. This task will run on the first of every month at 1:00am.
      AT 1:00am /every:1 Cscript.exe C:\Mailout.vbs 




  5. Depending on the scheduled time that you chose for the task above, your e-mail will be sent when the time occurs.


For more information on Microsoft's scripting technologies, please see the following Web site:
http://msdn.microsoft.com/scripting/

Additional query words:

Keywords :
Version : winnt:4.0,5.0
Platform : winnt
Issue type : kbhowto


Last Reviewed: February 2, 2000
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.