Tip

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, 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

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

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:

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.