Send Email from a Script

Scripts are great for automating tasks that you need to perform on a regular basis.  With a few lines of code you add the ability to send an email from within your scripts to notify you of a particular event.

From a programming standpoint, there are two ways to send a script programmatically.  The first involves using the Simple Mail Transport Protocol (SMTP) service installed on the computer where the script runs.  The second method involves using another SMTP server to forward you emails.  I prefer the latter since you only need one computer running SMTP to send emails instead of installing the service on a number of computers.

Before you can use the script you must locate a server running SMTP.  Check with your mail administrator and see if you can use one of the mail servers to forward emails.  If you don't have a server running SMTP you can add it to one of your existing servers.  In Windows Server 2003 SMTP is installed as part of the POP3 service.  To install the POP3 service open Control Panel > Add or Remove Programs > Add/Remove Windows Components then select Email Services.  Click the details button and check the box next to POP3 Service. 

Once the service has been installed you need to configure it so it will relay emails.  Open IIS Manager, right-click "Default SMTP Server" and select properties.  Click the "Access" tab and select the "Relay..." button.  Select the "Only the list below" radio button and configure the servers that will be allowed to relay emails on this server.

Now that you have an SMTP server you need a script.  The script below is the one I used.  I created a function called "SendEmail" and I pass it the subject (sSubject) and body text (sPageText) as arguments.  By creating a function, I can reuse this block of code in multiple scripts.  I have hard coded the "from address" (oEmail.From), "to address" (oEmail.To), and SMTP server (app1.lab.local).


Dim sPageText, sSubject

sPagetext = NOW() & "   The UPS has shutdown due to low battery.  UPSDOWN.CMD script was fired."

'Send Email
SendEmail sSubject, sPageText

Function SendEmail (sSubjectText,sBodyText)
    Dim oEmail
    Set oEmail = CreateObject("CDO.Message")
    oEmail.From = "ADMIN@lab.local"
    oEmail.To = ""
    oEmail.Subject = sSubjectText
    oEmail.Textbody = sBodyText
    oEmail.Configuration.Fields.Item _
     ("") = 2
    oEmail.Configuration.Fields.Item _
    ("") = _
    oEmail.Configuration.Fields.Item _
    ("") = 25



Sending E-Mail Without Installing the SMTP Service


Comments (0)

Skip to main content