Home > Scripting, VB Scripting > Sending An email via a script:

Sending An email via a script:

Recently i found myself in need of the ability to email a status script to my team. Not only this, but it would also need to attach an file. This script would run when a service stopped, start the service with a batch file doing a simple “net start ‘service'”, querying the service with psservice then call a VBS file to email the results of the net start command. This gave us visability of crappy software services stopping and the attempted automated restart.

I came across this script on our corporate network, cant give the credit as dont know where its due, but its excellent for a free script. More info after the jump, its quite a large script.


dtmDate = Date

strMonth = Month(Date)
strDay = Day(Date)
strYear = Right(Year(Date),2)

bFlag = CDOSYS_Send_Email(“<server>”,””,””,”<Recipient1@email.com;Recipient2@email.com>”,””,””,”<From address>”,”<Subject>””<Message Body>”,”<attachment>”)

Function CDOSYS_Send_Email(sSmtpServer,sUsername,sPassword,sTo,sCC,sBCC,sFrom,sSubject,sBody,sAttach)
On Error Resume Next
‘These weird constants do NOT cause any communications with schemas.microsoft.com. They are just naming conventions.
Const cdoSendUsingMethod = “http://schemas.microsoft.com/cdo/configuration/sendusing&#8221;
Const cdoSMTPServer = “http://schemas.microsoft.com/cdo/configuration/smtpserver&#8221;
Const cdoSMTPServerPort = “http://schemas.microsoft.com/cdo/configuration/smtpserverport&#8221;
Const cdoSMTPConnectionTimeout = “http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout&#8221;
Const cdoSMTPAuthenticate = “http://schemas.microsoft.com/cdo/configuration/smtpauthenticate&#8221;
Const cdoSendUserName = “http://schemas.microsoft.com/cdo/configuration/sendusername&#8221;
Const cdoSendPassword = “http://schemas.microsoft.com/cdo/configuration/sendpassword&#8221;
Const cdoSendUsingPickup = 1 ‘Use the local IIS-SMTP service for delivery.
Const cdoSendUsingPort = 2 ‘Use a remote SMTP server for delivery.

Set oMessage = WScript.CreateObject(“CDO.Message”)
Set oConfiguration = WScript.CreateObject(“CDO.Configuration”)

Set cFields = oConfiguration.Fields
cFields.Item(cdoSendUsingMethod) = cdoSendUsingPort ‘cdoSendUsingPort = Send to remote SMTP server (2), cdoSendUsingPickup = Send using local SMTP service (1).
cFields.Item(cdoSMTPServer) = sSmtpServer ‘IP address or DNS name of the remote SMTP server.
cFields.Item(cdoSMTPServerPort) = 25
cFields.Item(cdoSMTPConnectionTimeout) = 30 ‘Timeout in seconds for connection.

If LCase(sUsername) = “ntlm” Then
cFields.Item(cdoSMTPAuthenticate) = 2 ‘2 = NTLM authentication. NTLM uses the credentials of the WSH process itself.
ElseIf sUsername <> “” Then
cFields.Item(cdoSMTPAuthenticate) = 1 ‘1 = Basic authentication.
cFields.Item(cdoSendUserName) = sUsername ‘Username for Basic authentication, ignored for anonymous or NTLM authentication.
cFields.Item(cdoSendPassword) = sPassword ‘Password for Basic authentication, ignored for anonymous or NTLM authentication.
cFields.Item(cdoSMTPAuthenticate) = 0 ‘0 = Anonymous authentication.
End If

cFields.Update ‘Save data so far.
Set oMessage.Configuration = oConfiguration

oMessage.To = sTo
oMessage.CC = sCC
oMessage.BCC = sBCC
oMessage.From = sFrom
oMessage.Subject = sSubject
oMessage.TextBody = sBody

If sAttach <> “” Then
aFiles = Split(sAttach,”;”)
For Each sFile In aFiles
oMessage.AddAttachment sFile
End If


If Err.Number = 0 Then
CDOSYS_Send_Email = True
CDOSYS_Send_Email = False
End If

Set oMessage = Nothing
Set oFields = Nothing
Set oConfiguration = Nothing
End Function

‘END OF SCRIPT *********************************************************************


The above variables seem to translate to these:

Arguments: sSmtpServer = IP address or FQDN of out-going SMTP server.
‘ sUsername = (May be blank.) Username for Basic authentication; set blank for Anonymous; set ntlm for NTLM.
‘ sPassword = (May be blank.) Password for Basic authentication; ignored for Anonymous and NTLM.
‘ sTo = Recipient. Semicolon-delimited for multiple addresses.
‘ sCC = (May be blank.) CC recipient. Semicolon-delimited for multiple addresses.
‘ sBCC = (May be blank.) Blind CC recipient. Semicolon-delimited for multiple addresses.
‘ sFrom = Single e-mail address for sender.
‘ sSubject = (May be blank.) Subject line.
‘ sBody = (May be blank.) Body of message.
‘ sAttach = (May be blank.) Full path to local file to attach. Semicolon-delimited for multiple addresses.

if you are having difficulty with sending the email, ensure the server/desktop you run this script from can telnet to the exchange/smtp server on port 25. to test this use:

telnet <smtp address> 25


Below is a working excerpt of my script:

CDOSYS_Send_Email(“″,””,””,”Andrew.Morgan@o2.com”,”SpaceChecks for DailyChecklist”,”E:\NT Infrastructure\Daily Checks\Dublin\SpaceResults\” & strMonth & “-” & strDay & “-” & strYear & “.xls”)

Happy coding =)

Categories: Scripting, VB Scripting Tags: ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: