A little while ago, I wrote a post on how to send an email using JScript and the very useful program - NetCat. This post covers the same ground, but in VBScript, and compares the code in the two languages.
The original post is here and also on 'The Code Project' here.
I find that JScript is easier to work in JScript than VBScript for most of the stuff I do; however, many people prefer VBScript. The snag is that Microsoft does not seem to be putting much effort into supporting VBScript; they have not even created (as far as I know) a .NET version of it. I guess this means that there is some benefit in VBScripters being about to code in JScript as well. To that end, I have not only given and discussed the VBScript source here, but also shown (in comments) the JScript of the original. I hope that those less familiar with JScript can see that it is really very similar to VBScript once you've gotten over all those wiggly brackets!
How Does it Work
I have already discussed interacting with Web Services using the
XMLHTTP object. For sending email, there is no such ready made object to help us. However, sending email is not an especially complex matter, so we can just use NetCat instead!
To do this, you will first require a copy of NetCat. I have a copy that runs on Windows XP from here. I did not do the port, but the credits are in the zip, and it is GPL 2. Once you have nc.exe, you will need to put it somewhere on the executable path (e.g., system32) or in the same directory in which you are going to run your script.
The script should be run by cscript from the command prompt. You will have to change the address of the SMTP server to one for which you have access, and set the to and from email addresses, hit count, and inter-hit delay to the appropriate settings. This is a real script, I used it today to load test an email server!
strExe = "nc -v smtp.gotadsl.co.uk 25"
set objShell = WScript.CreateObject("WScript.Shell")
total = 8
delay = 0
victim = "email@example.com"
for i=1 to total
strExeIn = "HELO nerds-central.com" + vbcrlf
strExeIn = strExeIn + "MAIL FROM: <firstname.lastname@example.org>" + vbcrlf
strExeIn = strExeIn + "RCPT TO: <"+victim+">" + vbcrlf
strExeIn = strExeIn + "DATA"+ vbcrlf
strExeIn = strExeIn + _
"Body of email: this is an auto generated test email." + _
strExeIn = strExeIn + "This is " & i & " of " & total & vbcrlf
strExeIn = strExeIn + "." + vbcrlf
strExeIn = strExeIn + "QUIT" + vbcrlf
set objScriptExec = objShell.Exec(strExe)
WScript.echo("Sending " & i & " of " & total & " to "+victim)
For loads more on VBScript, see the VBScript section of Nerds-Central.