Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How Can I Run VBScript Without using A Browser 4

Status
Not open for further replies.

Skittle

ISP
Sep 10, 2002
1,528
US
Many years ago I built a set of ASP pages to access databases and display dynamic results on web pages.

I now want to perform a few automated tasks at regular intervals on a PC. One of these tasks is to send a email.

Can VBScript be used to regularly perform a batch job such as sending an email? If so how do you execute a .asp script file? Do I have to install some sort of shell program to be able to run the script from DOS as a scheduled task?


Dazed and confused
 
You can use CDO to send an email using vbscript. You wouldn't need an ASP file. In your scheduled task you would have it execute a command like this.

cscript.exe yourscript.vbs

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
What is CDO?

Anybody have an example of a .vbs sending an email?



Dazed and confused
 
...found an example:-

Set objMessage = CreateObject("CDO.Message")
objMessage.Subject = "Example CDO Message"
objMessage.From = "me@my.com"
objMessage.To = "test@paulsadowski.com"
objMessage.TextBody = "This is some sample message text."
objMessage.Send

My last question is, do I have to have anything other than a standard Outlook email client set up on the PC that is scheduled to run the script?


Dazed and confused
 
In fact I'm intrigued.

It works fine but I'd love it if somebody could fill me in on how it works. When I've sent messages from SQL server 2000 in the past, I had to set up a client Outlook configuration on the server. A MAPI client is the term I believe.

With the vbscript method, what client email software does the sending machine use to send the email? How does it know which SMTP server to use when connected to the network?
Hope this isn't to dim a question :eek:)


Dazed and confused
 
This subroutine will allow you to send an e-mail via an SMTP server, you can put it into a .vbs file, it can be altered to send an HTML email or to include one of more attachements

Code:
Sub SendEmail(strTo, strFrom, strSubject, strMsg)
	schema = "[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/"[/URL]
	SmtpServer	= "" 'Put your SMTP server name or address here.

	Set objEmail = CreateObject("CDO.Message")
	objEmail.From = strFrom
	objEmail.To = strTo
	objEmail.Subject = strSubject
	objEmail.Textbody = strMsg
	
	objEmail.Configuration.Fields.Item (schema & "sendusing") = 2
	objEmail.Configuration.Fields.Item (schema & "smtpserver") = SmtpServer
	objEmail.Configuration.Fields.Item (schema & "smtpserverport") = 25
	objEmail.Configuration.Fields.Update
	objEmail.Send
End Sub


To add an attachment include this line: (you can add multiple lines for multiple attachments.)
Code:
objEmail.AddAttachment "C:\Report.csv"

To send HTML email use this:
Code:
'Swap this line
objEmail.Textbody = strMsg

'With this one
objEmail.HTMLbody = strMsg

To use, just call the sub passing the To, From, Subject and message parameters and don't forget to set the name or IP address of the SMTP server.

Obviously you need access to an SMTP server to be able to use this script.
 
Hi Skittle,

You do not need an outlook client running for this to work. This CDO object lets you send an email using a remote SMTP server and is available by default.

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
There are several "CDOs" though most descriptions assume CDO for Windows (originally CDO for Windows 2000) rather than the second most common one CDO for Exchange.

CDO can be used for both MAPI and SMTP message sending. This can lead to confusion when examining and testing sample code, so it helps a lot to read the documentation first.
 
Hey Skittle.
The script below is a file called email.vbs.
If you have windows scripting host installed (cscript)
you can just double click on this file. I use MAPI.
This script also attaches a .csv file

Dim ToAddress
Dim FromAddress
Dim MessageSubject
Dim MessageBody
Dim MessageAttachment
Dim ol, ns, newMail
ToAddress = "cindy@email.com"
MessageSubject = "Greetings"
MessageBody = "Here are your leads for 05-16-2007"
MessageAttachment = "Y:\folder\folder\16216.CSV"
Set ol = WScript.CreateObject("Outlook.Application")
Set ns = ol.getNamespace("MAPI")
Set newMail = ol.CreateItem(olMailItem)
newMail.Subject = MessageSubject
newMail.Body = MessageBody & vbCrLf
newMail.RecipIents.Add(ToAddress)
newMail.Attachments.Add(MessageAttachment)
newMail.Send


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top