Send email using CDO (VBScript)

AddThis Social Bookmark Button

If you have servers to monitor and require automated email notifications then you need a VBScript that can be scheduled and run automatically. This script allows you to take any HTML file and send it as the content of an email and include another file as an attachment. I have used this in conjunction with scripts that generate server storage space using WMI so I can monitor disk usage. This email can constitute a daily or weekly report simplifying your management and monitoring. This script uses CDO to send mail and can be configured with or without authentication.

To trigger this script from a scheduled task it is recommended to use a command./batch file which is included with 32 bit and 64 bit example. Simply comment out the unrequired section depending on your environment. This script is designed to be run from your C drive but can be easily modified. Remember to configure your email settings before use. I have highlighted the sections you need to modify.



Dim HtmlBody 'To store html body
Dim EmailSubject 'To store email Subject
Dim EmailFrom 'To store email From
Dim EmailTo 'To store email To
Dim EmailBcc 'To store email BCC
Dim EmailCc 'To store email CC
Dim EmailSMTPServer 'To store SMTP Server name or address
Dim fso 'To declare File System Object for file access
Dim filename 'To declare filename for email content
Dim myattachment 'To declare filename for attachment

'Initialization of Email Configuration
EmailSMTPServer = ""
EmailSubject = "My Email"
EmailFrom = "This email address is being protected from spambots. You need JavaScript enabled to view it."
EmailTo = "This email address is being protected from spambots. You need JavaScript enabled to view it."
EmailBcc = ""
EmailCc = ""

'declare file to use as HTML email content
filename = "c:\myemail.html"

'declare file to use as attachment
myattachment = "c:\myemail.html"

'Creating File System Object and loading HTML file into email content
Set fso = CreateObject("Scripting.FileSystemObject")
Set ObjOutFile = fso.OpenTextFile(filename,1)
HtmlBody = objOutFile.ReadAll

'Send the HTML email

'Specify Email sending properties
Function GetCDOFromProperties()
Dim SMTPServer, SMTPPort, SMTPAuthenticate, SMTPUserName, SMTPPassword, SMTPTimeout, SMTPProxyServer, SMTPProxyBypass
SMTPServer = EmailSMTPServer
SMTPPort = "25"
SMTPAuthenticate = "0"
SMTPUserName = ""
SMTPPassword = ""
SMTPTimeout = "5000"
SMTPProxyServer = ""
SMTPProxyBypass = ""

Set GetCDOFromProperties = ConfigureCDO(SMTPServer, SMTPPort, SMTPAuthenticate, SMTPUserName, SMTPPassword, SMTPTimeout, SMTPProxyServer, SMTPProxyBypass)

End Function

'Configure CDO Email
Function ConfigureCDO(SMTPServer, SMTPPort, SMTPAuthenticate, SMTPUserName, SMTPPassWord, SMTPTimeout, SMTPProxyServer, SMTPProxyBypass)
Dim Conf

' Setup SMTP Options
Set Conf = CreateObject("CDO.Configuration")

'Specifies the method used to send messages. 1 - Pickup; 2 - Port. If a local SMTP service is available, this field defaults to 1
Conf.Fields.Item("") = 2

' The pickup directory for the local SMTP service. This value is set automatically when the SMTP service is installed.
'Conf.Fields.Item("") = "c:\mailroot\pickup"

' The name (DNS) or IP address of the machine hosting the SMTP service through which messages are to be sent.
Conf.Fields.Item("") = strnullcheck(SMTPPort)
' The port on which the SMTP service specified by the smtpserver field is listening for connections. The default and well-known port for an SMTP service is 25.
Conf.Fields.Item("") = strnullcheck(SMTPServer)

' SMTP is using SSL; Default False
'Conf.Fields.Item("") = False

' 0 - Do not auth; 1 - Basic (clear text auth); 2 - Use NTLM authentication (Secure Password Authentication in Microsoft® Outlook® Express) The current process security context is used to authenticate with the service.
Conf.Fields.Item("") = strnullcheck(SMTPAuthenticate)

Conf.Fields.Item("") = strnullcheck(SMTPUserName)
Conf.Fields.Item("") = strnullcheck(SMTPPassWord)
' The user's email address used when connecting to the SMTP service.
'Conf.Fields.Item("") = "This email address is being protected from spambots. You need JavaScript enabled to view it."

' Indicates the number of seconds to wait for a valid socket to be established with the SMTP service before timing out. Default is 30
Conf.Fields.Item("") = strnullcheck(SMTPTimeout)

' The proxy server to use when accessing HTTP resources. Both the name (or IP address) of the proxy machine and the port number must be specified using the format "servername:port."
Conf.Fields.Item("") = strnullcheck(SMTPProxyServer) '"server:80"

' Used to specify that for local addresses, the proxy (set with urlproxyserver) should be bypassed. The value of the string, if set, should only be "<local>". This is the same value used with Internet Explorer when setting a proxy with bypass for local addresses.
Conf.Fields.Item("") = strnullcheck(SMTPProxyBypass) '"<local>"


Set ConfigureCDO = Conf
End Function

'Used to check for blank values in CDO Configuration
Function strnullcheck(varString)
If Isnull(varString) Then
strnullcheck = ""
strnullcheck = varString
End If
End Function

'Send the email (including an attachment)
Function SendEmail(HtmlBody)
Set objMessage = CreateObject("CDO.Message")
objMessage.Configuration = GetCDOFromProperties()

'Email header
objMessage.Subject = EmailSubject
objMessage.From = EmailFrom
objMessage.To = EmailTo
objMessage.Bcc = EmailBcc
objMessage.Cc = EmailCc

'Add Email Attachment
objMessage.AddAttachment myattachment

'The line below shows how to send using HTML included directly in your script
objMessage.HTMLBody = HtmlBody


Set objMessage = nothing
HtmlBody = ""
End Function


REM 32 bit start script
REM "C:\Windows\System32\cscript.exe" "c:\sendemail.vbs"

REM 64 bit start script

"C:\Windows\Syswow64\cscript.exe" "c:\sendemail.vbs"
Download this file ( kB2013-04-30

Only members can add comments. Becoming a member is free. See the Members section for more information.