HOWTO: Create Scheduled Email Messages Using WSH and CDONTSID: Q221495
|
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.
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.
<p>This is line 1.</p>
<p>This is line 2.</p>
'--------------------------------------------------------------------
'
' 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
AT 9:00pm Cscript.exe C:\Mailout.vbs
AT 6:00am /every:M Cscript.exe C:\Mailout.vbs
AT 1:00am /every:1 Cscript.exe C:\Mailout.vbs
http://msdn.microsoft.com/scripting/
Additional query words:
Keywords :
Version : winnt:4.0,5.0
Platform : winnt
Issue type : kbhowto
Last Reviewed: July 16, 1999