You are currently offline, waiting for your internet to reconnect

Sending E-mail from a Command Prompt Using the IIS SMTP Service

This article was previously published under Q193685
We strongly recommend that all users upgrade to Microsoft Internet Information Services (IIS) version 6.0 running on Microsoft Windows Server 2003. IIS 6.0 significantly increases Web infrastructure security. For more information about IIS security-related topics, visit the following Microsoft Web site:
The Windows NT Option Pack (NTOP) includes a Simple Mail Transfer Protocol(SMTP) service for Internet Information Server (IIS). Also included in theOption Pack is the Windows Script Host (WSH), which is a powerful batchprocessing language interpreter, allowing administrators to write scriptsin Visual Basic Script (VBS) or JScript (JS). Together the SMTP serviceand WSH can be used to send e-mail from a command line.
With the SMTP service installed, a new system object named"CDONTS.NewMail" can be used to send mail with a few lines of code.

The following example using VBS and WSH demonstrates how to send -emailfrom a command prompt. Save the code shown below as SENDMAIL.VBS, open acommand prompt session. and call the code using the following syntax:

This will return the Help screen that shows the syntax for using thescript. This script can be used inside batch files, or for other purposessuch as sending alerts from Performance Monitor.

Save the following VBS code as SENDMAIL.VBS:
   '--------------------------------------------------   '   ' Sends email from the local SMTP service using CDONTS objects   '   ' Usage:   '   sendmail -t <to> -f <from> -s "<subject>" -b "<message>"   '   sendmail [-help|-?]   '   '--------------------------------------------------   Option Explicit   On Error Resume Next   Dim objSendMail, oArgs, ArgNum   Dim strTo, strFrom, strSubject, strBody   Set oArgs = WScript.Arguments   ArgNum = 0   While ArgNum < oArgs.Count      Select Case LCase(oArgs(ArgNum))         Case "-to","-t":            ArgNum = ArgNum + 1            strTo = oArgs(ArgNum)         Case "-from","-f":            ArgNum = ArgNum + 1            strFrom = oArgs(ArgNum)         Case "-subject","-s":            ArgNum = ArgNum + 1            strSubject = oArgs(ArgNum)         Case "-body","-b":            ArgNum = ArgNum + 1            strBody = oArgs(ArgNum)         Case "-help","-?":            Call DisplayUsage         Case Else:            Call DisplayUsage      End Select      ArgNum = ArgNum + 1   Wend   If oArgs.Count=0 Or strTo="" Or strFrom="" Or _         strSubject="" Or strBody="" Then      Call DisplayUsage   Else      Set objSendMail = CreateObject("CDONTS.NewMail")         objSendMail.From = strFrom         objSendMail.To = strTo         objSendMail.Subject = strSubject         objSendMail.Body = strBody         objSendMail.Send      Set objSendMail = Nothing   End If   ' Display the usage for this script   Sub DisplayUsage      WScript.Echo "Usage:"      WScript.Echo "  sendmail -t <to address> -f <from address> -s " & _         Chr(34) & "<subject>" & Chr(34) & " -b " & Chr(34) & _         "<message body>" & Chr(34)      WScript.Echo "  sendmail [-help|-?]"      WScript.Echo ""      WSCript.Quit   End Sub				

Article ID: 193685 - Last Review: 06/23/2005 21:39:26 - Revision: 3.3

Microsoft Internet Information Services 6.0, Microsoft Internet Information Services 5.0, Microsoft Internet Information Server 4.0

  • kbhowto KB193685