' SendMail.vbs V-1.00 Michael Bednarek, June 2003 ' Arguments: Subject Text to1 to2 ... ' If Subject or Text contain whitespace they must be enclosed in quotes ' If Text is a filename, its content will be used as text. ' Recipients may be specified as "Display name " Option Explicit Dim colArgs ' as WshArguments Collection Dim fileSpec ' as String Dim objFSO ' as FileSystemObject Dim fsoStream ' as TextStream Dim theBody ' as String Dim objMailItem ' as Outlook.Mailitem Dim myError ' as Long Dim rc ' as Long Dim i ' as Long Const ForReading = 1 Const olMailItem = 0 Const olTo = 1 Set colArgs = WScript.Arguments If colArgs.Count < 3 Then WScript.Echo "SendMail.vbs must have at least 3 arguments:" WScript.Echo "Subject Text|filename Recipient1 [Recipient2 ...]" & vbNewLine WScript.Echo "If an argument contains spaces, it must be enclosed in quotes." WScript.Echo "Recipients may be specified as ""Display name """ Set colArgs = Nothing WScript.Quit 8 End If fileSpec = colArgs(1) ' The 2nd argument is the body. Check whether such a file exists. Set objFSO = CreateObject("Scripting.FileSystemObject") If (objFSO.FileExists(fileSpec)) Then Set fsoStream = objFSO.OpenTextFile(filespec, ForReading) theBody = fsoStream.ReadAll fsoStream.Close Set fsoStream = Nothing Else theBody=fileSpec End If Set objFSO = Nothing Set objMailItem = CreateObject("Outlook.Application").CreateItem(olMailitem) myError = 0 rc = 0 ' Number of resolved recipients With objMailItem .Subject = colArgs(0) ' The 1st argument is the suject. .Body = theBody For i = 2 To colArgs.Count -1 ' The 3rd and further arguments is/are the recipient/s rc = rc + 1 .Recipients.Add(colArgs(i)) .Recipients(rc).Type = olTo If .Recipients(rc).Resolve = False Then WScript.Echo "Can't resolve " & colArgs(i) .Recipients(rc).Delete myError = 2 rc = rc -1 End If Next End With Set colArgs = Nothing If rc > 0 Then ' Any recipients resolved? objMailItem.Send Set objMailItem = Nothing Else WScript.Echo "No recipients could be resolved. Nothing done." myError = 4 End If WScript.Quit myError