VBScript does mail

You can send e-mail messages from your script, as this tip explains.

Please let us know how useful you find this tip by rating it below!


Use this sub to send e-mail messages from your script. This script works on Windows 2000, and may work on NT 4.0 if the correct CDO library is installed.

This sub requires the SMTPserver, From address, Recipient address, Subject, and Message be passed as arguments.

A couple of general comments:

  • The SMTP service does not need to be running on the same machine as the script.
  • Separate multiple recipients with a comma or semi-colon. For example: someone@host1.com,someoneelse@host2.com

' Begin example
SMTPServer = "smtphost.domain.com"
Recipient = "you@yours.com"
From = "me@mine.com"
Subject = "Test email"
Message = "This is a two line test message" & vbcrlf & "Did you get it?"

' To add an attachment update the full path and uncomment the line
' There is one line below that must also be uncommented


'attachment = "c: \ test.txt"

' Call Sub and pass required data
GenericSendmail SMTPserver, From, Recipient, Subject, Message

' Begin Sub
' ------------------------------------------------------------------
' Generic function to send mail using a remote
' SMTP server. Pass SMTPserver, From address,
' Recipient address, Subject, and Message as arguments
' ------------------------------------------------------------------
Sub GenericSendmail (SMTPserver, From, Recipient, Subject, Message)

set msg = WScript.CreateObject("CDO.Message")
msg.From = From
msg.To = Recipient
msg.Subject = Subject
msg.TextBody = Message

' To add an attachment uncomment this line
'msg.AddAttachment attachment

msg.Configuration.Fields ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = SMTPServer
msg.Configuration.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2

msg.Configuration.Fields.Update

msg.Send

End Sub


Patrick Sklodowski is a Microsoft Certified Systems Engineer with over eight years of industry experience. His specialties include Windows NT/2000, Active directory, SMS, Exchange and scripting. He is currently working as a Senior Engineer with a global provider of engineering solutions and specialized staffing.
This was first published in January 2005
This Content Component encountered an error

Pro+

Features

Enjoy the benefits of Pro+ membership, learn more and join.

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

-ADS BY GOOGLE

SearchVirtualDesktop

SearchWindowsServer

SearchExchange

Close