Send mail from a Windows script

Jörgen Lundberg picture Jörgen Lundberg · Sep 30, 2008 · Viewed 48.3k times · Source

I would like to send mail from a script on a Windows Server 2003 Standard Edition. I think the server setup is pretty much out of the box.

The mail server is an Exchange one, and when you're on the internal network you can use plain old SMTP. I have done it from my machine with Perl, but unfortunately Perl is not available on the server.

Is there an easy way of doing this from a .bat-file or any other way that doesn't require installing some additional software?

Edit:
Thanks for the quick replies. The "blat" thingie would probably work fine but with wscript I don't have to use a separate binary.

I didn't see PhiLho's post the first time I edited and selected an answer. No need for me to duplicate the code here.

Just save the script to a file, say sendmail.vbs, and then call it from the command prompt like so:
wscript sendmail.vbs

Answer

PhiLho picture PhiLho · Sep 30, 2008

It is possible with Wscript, using CDO:

Dim objMail

Set objMail = CreateObject("CDO.Message")

objMail.From = "Me <[email protected]>"
objMail.To = "You <[email protected]>"
objMail.Subject = "That's a mail"
objMail.Textbody = "Hello World"
objMail.AddAttachment "C:\someFile.ext"

---8<----- You don't need this part if you have an active Outlook [Express] account -----
' Use an SMTP server
objMail.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2

' Name or IP of Remote SMTP Server
objMail.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = _
    "smtp.server.com"

' Server port (typically 25)
objMail.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25

objMail.Configuration.Fields.Update
----- End of SMTP usage ----->8---

objMail.Send

Set objMail=Nothing
Wscript.Quit

Update: found more info there: VBScript To Send Email Using CDO By default it seems it uses Outlook [Express], so it didn't worked on my computer but you can use a given SMTP server, which worked fine for me.