HOW TO: Create Scheduled E-mail Messages by Using WSH and CDONTS (221495)
The information in this article applies to:
- Microsoft Internet Information Server 4.0
- Microsoft Internet Information Services 5.0
This article was previously published under Q221495 We strongly recommend that all users upgrade to Microsoft Internet Information Services (IIS) version 6.0 running on Microsoft Windows Server 2003. IIS 6.0 significantly increases Web infrastructure security. For more information about IIS security-related topics, visit the following Microsoft Web site: SUMMARY
By default, Microsoft Windows NT 4.0 with the Windows NT Option Pack (NTOP) and Microsoft Windows 2000 install a Simple Mail Transfer Protocol (SMTP) service that can be used for a variety of purposes. One of the more common uses is Active Server Pages (ASP) for e-mail-based HTML form handling.
This step-by-step article describes how to create scheduled mail-outs by using Windows Script Host (WSH) and the e-mail features of the SMTP service CDONTS.NewMail object. The example shows how to create a text file that you can update for the e-mail message contents, a WSH script to send an e-mail message, and a scheduled task to schedule the e-mail message to be sent.
back to the top
Verify that the Scheduler Service Is Running- On a computer that is running Windows NT 4.0 and Internet Information Server (IIS) 4.0:
- Click Start, click Settings,
and then click Control Panel.
- In Control Panel, double-click Services.
- Scroll down to the Task Scheduler service.
- Verify that Status indicates Started. If Status is not set to Started, click to select Task Scheduler and then click Start.
- Verify that Startup indicates Automatic. If Startup is not set to Automatic, click to select Task Scheduler and then click Startup. Under Startup Type, click to select Automatic, and then click OK.
- Click Close.
- To exit, close Control Panel.
- On a computer that is running Windows 2000 and Internet Information Services (IIS) 5.0:
- Click Start, click Settings,
and then click Control Panel.
- Double-click Administrative Tools, and then double-click Computer Management.
- In the left pane of the Computer Management window, expand Services and Applications, and then click to select Services.
- In the right pane of the Computer Management window, scroll through the list of services in the Name column.
- Scroll down to the Task Scheduler service.
- Verify that Status indicates Started. If the service is not started, right-click the Task Scheduler service, and then click to select Start.
- Verify that Startup Type indicates Automatic. If Startup Type is not set to Automatic, right-click the Task Scheduler service, and then click Properties. The service_name window appears with the General tab selected. Note that you can click the other tabs at the top of the window to see other options. Click the drop-down arrow next to Startup type, and then select Automatic.
- Click OK.
- To exit, close the Computer Management window.
back to the top
Create the E-Mail File- Open Notepad. To do this, click Start, click Run, type NOTEPAD, and then click OK.
- Paste the following lines in Notepad:
<p>This is line 1.</p>
<p>This is line 2.</p>
- In Notepad, click the File menu, click Save As, type C:\Mailout.txt in the File name text box, and then click OK.
- To exit, close Notepad.
back to the top
Create the WSH Script- Open Notepad. To do this, click Start, click Run, type NOTEPAD, and then click OK.
- Paste the following lines in Notepad:NOTE: You must specify a valid e-mail address. To do this, customize the sender and receiver by changing the value of the strFrom and strTo variables in the following code:
'--------------------------------------------------------------------
'
' 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 e-mail address.
strTo = "someone@microsoft.com" ' Change to the recipient 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 contents of the file.
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
- In Notepad, click the File menu, click Save As, type C:\mailout.vbs in the File name text box, and then click OK.
- To exit, close Notepad.
back to the top
Create a Task to Schedule the Mail-Out to Be Sent- Click Start, click Run, type CMD, and then click OK to open a command prompt.
- At the command prompt window, type something similar to the following examples:
- The following task runs one time at 9:00 P.M.
AT 9:00pm Cscript.exe C:\Mailout.vbs - This task runs every Monday at 6:00 A.M.
AT 6:00am /every:M Cscript.exe C:\Mailout.vbs - This task runs on the first of every month at 1:00 A.M.
AT 1:00am /every:1 Cscript.exe C:\Mailout.vbs NOTE: Depending on which of these examples you choose, the e-mail message is sent at the scheduled time.
- Type EXIT to close the command prompt.
back to the top
REFERENCES
For more information about Microsoft scripting technologies, visit the following Microsoft Developer Network (MSDN) Web site:
back to the top
Modification Type: | Minor | Last Reviewed: | 6/23/2005 |
---|
Keywords: | kbhowto kbHOWTOmaster KB221495 |
---|
|