SunQuest
 
       Windows Scripting
  Home arrow Windows Scripting arrow Page 3 - Logon Script to Send Email Notifications
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Actuate Whitepapers 
Moblin 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
WINDOWS SCRIPTING

Logon Script to Send Email Notifications
By: Nilpo
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 3
    2008-01-29

    Table of Contents:
  • Logon Script to Send Email Notifications
  • Gathering logon details
  • Creating and sending the notification email
  • Putting it all together

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Ajax Application Generator Generate database and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!

    Logon Script to Send Email Notifications - Creating and sending the notification email


    (Page 3 of 4 )

    Now that we have all of the information that we need, it’s time to write a function that will create and send an email with it.  I’ll keep the explanations here brief.  If you would like more information, I have an entire article dedicated to the topic.

    Sending Emails Using CDO in WSH

    In short, this function needs to create an email message and send it using the details provided.  Most of the details need to be provided as constants at the beginning of the script.  The body of the email will be passed as arguments to the function.

    Function SendMail(strBody)

       Set objEmail = CreateObject("CDO.Message")

       With objEmail

           .From = ADMIN_EMAIL

           .To = ADMIN_EMAIL

           .Subject = "Logon Notification"

           .HTMLBody = strBody

    The function begins by connecting to the CDO object and assembling the message object to send.  Like any email, it requires a To and From address along with a subject line and body.

           .Configuration.Fields.Item _

       ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2

           .Configuration.Fields.Item _

       ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = SMTP_SERVER

           .Configuration.Fields.Item _

       ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = SMTP_PORT

           If USE_AUTHENTICATION Then

              .Configuration.Fields.Item _

       ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1

              .Configuration.Fields.Item _

       ("http://schemas.microsoft.com/cdo/configuration/sendusername") = SMTP_USER

              .Configuration.Fields.Item _

       ("http://schemas.microsoft.com/cdo/configuration/sendpassword") = SMTP_PASS

     

           End If

           If SMTP_SSL Then

              .Configuration.Fields.Item _

       ("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True

     

           End If

           .Configuration.Fields.Update

    The next section of code sets all of the required SMTP server settings for the CDO object to use.  I’ve allowed for both simple and authenticated SMTP access.  Variables at the beginning of the script are used to set these requirements.

           On Error Resume Next

           Err.Clear

     

           .Send

     

           If Err.number <> 0 Then

              SendMail = Err.Description

     

           Else

              SendMail = "The server did not return any errors."

     

           End If

           On Error Goto 0

     

       End With

     

    End Function

    We end our function by actually sending the email message.  A little error-handling ensures that the script doesn’t break if for some reason the mail cannot be sent.  With a little tweaking, you could easily make this function retry after a period of time, if this were to happen.

    More Windows Scripting Articles
    More By Nilpo


     

    WINDOWS SCRIPTING ARTICLES

    - Understanding Procedures in VBScript
    - Printing Documents in WSH
    - Generating Outlook Signatures Based on Activ...
    - VBScript: Converting and Formatting with Fun...
    - VBScript: Conversion and Format Functions
    - VBScript: Array Functions
    - VBScript: Strings, You Can`t Function withou...
    - VBScript: More String Functions
    - VBScript: Functioning with Strings
    - Working with the Windows Registry in C++
    - Understanding Objects
    - HTML Applications: Giving WSH a User Interfa...
    - Modifying Computer Objects with Active Direc...
    - Logon Script to Send Email Notifications
    - Securing Computers and Active Directory





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway