VBScript does mail

VBScript does mail

Patrick Sklodowski, Contributor

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,

    Requires Free Membership to View

    When you register, you’ll also receive targeted alerts from my team of editorial writers and independent industry experts with the latest news, tips, and advice to help you do your job more efficiently and effectively. Our goal is to keep you informed on the hottest topics and biggest challenges faced by IT professionals today working with desktop management and security technologies.

    Margie Semilof, Editorial Director

    By submitting your registration information to SearchEnterpriseDesktop.com you agree to receive email communications from TechTarget and TechTarget partners. We encourage you to read our Privacy Policy which contains important disclosures about how we collect and use your registration and other information. If you reside outside of the United States, by submitting this registration information you consent to having your personal data transferred to and processed in the United States. Your use of SearchEnterpriseDesktop.com is governed by our Terms of Use. You may contact us at webmaster@TechTarget.com.

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

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.