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  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
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 / 4
    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


    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

    - More Windows Scripting Workarounds from Nilpo
    - Overloading Methods and More in VBScript
    - Improving MFC for Windows Vista
    - Regular Expressions in VBScript
    - Working with Dates in WMI
    - Completing Calendars with VBScript Date Func...
    - Building Calendars with VBScript Date Functi...
    - Working With Dates and Times in VBScript
    - Designing WCF DataContract Classes Using the...
    - Understanding Dates and Times in VBScript
    - Working With Arrays in VBScript
    - Compressed Folders in WSH
    - Using .NET Interops in VBScript
    - Nilpo`s Scripting Secrets, Vol I
    - Database operations using Silverlight 2.0 WC...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek