Mardi 10 juin 2008
'--------------------------------------------------
'
' Sends email from the local SMTP service using CDONTS objects
'
' Usage:
' sendmail [-smtp <server>] [-port <port>] [-auth <0=None, 1=Basic, 2=NTLM>] [-user <user>] [-pass <password>]
' -t <to> -f <from> -s "<subject>" -b "<message>" -a "<attachment1>,<attachment2>,..,<attachmentN>"
' sendmail [-help|-?]
'
'--------------------------------------------------
Option Explicit
On Error Resume Next
Dim oArgs, ArgNum
Dim strServer, intPort, intAuth, strUser, strPass, strTo, strFrom, strSubject, strBody, strAttachments
Dim arrAttachments, strAttachment
Dim strSch, objConfig, objMessage
Set oArgs = WScript.Arguments
ArgNum = 0
strServer = "localhost"
intPort = 25
intAuth = 0
While ArgNum < oArgs.Count
Select Case LCase(oArgs(ArgNum))
Case "-smtp"
ArgNum = ArgNum + 1
strServer = oArgs(ArgNum)
Case "-port"
ArgNum = ArgNum + 1
intPort = oArgs(ArgNum)
Case "-auth"
ArgNum = ArgNum + 1
intAuth = oArgs(ArgNum)
Case "-user"
ArgNum = ArgNum + 1
strUser = oArgs(ArgNum)
Case "-pass"
ArgNum = ArgNum + 1
strPass = 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 "-attachment","-a":
ArgNum = ArgNum + 1
strAttachments = oArgs(ArgNum)
Case "-help","-?":
Call DisplayUsage
Case Else:
Call DisplayUsage
End Select
ArgNum = ArgNum + 1
Wend
WScript.Echo strAttachments
If oArgs.Count=0 Or strTo="" Or strFrom="" Or _
strSubject="" Or strBody="" Then
Call DisplayUsage
Else
strSch = "http://schemas.microsoft.com/cdo/configuration/"
Set objConfig = CreateObject("CDO.Configuration")
With objConfig.Fields
.Item(strSch & "sendusing") = 2 ' cdoSendUsingPort
.Item(strSch & "smtpauthenticate") = intAuth
.Item(strSch & "smtpserver") = strServer
.Item(strSch & "smtpserverport") = intPort
If intAut = 1 Then
.Item(strSch & "sendusername") = strUser
.Item(strSch & "sendpassword") = strPass
End If
.update
End With
Set objMessage = CreateObject("CDO.Message")
With objMessage
Set .Configuration = objConfig
.From = strFrom
.To = strTo
.Subject = strSubject
.TextBody = strBody
IF strAttachments <> "" Then
arrAttachments = Split(strAttachments, ",", -1, 1)
For Each strAttachment in arrAttachments
.AddAttachment(strAttachment)
Next
Set arrAttachments = Nothing
End If
.Send
End With
Set objMessage = Nothing
Set objConfig = Nothing
End If
' Display the usage for this script
Sub DisplayUsage
WScript.Echo "Usage:" & _
Chr(13) & Chr(13) & _
"sendmail [-smtp <server>] [-port <port>] [-auth <0=None, 1=Basic, 2=NTLM>] [-user <user>] [-pass <password>]" & _
Chr(13) & _
" -t <to address> -f <from address>" & _
Chr(13) & _
" -s " & Chr(34) & "<subject>" & Chr(34) & " -b " & Chr(34) & "<message>" & Chr(34) & _
Chr(13) & _
" -a " & Chr(34) & "<attachment1>,<attachment2>,..,<attachmentN>" & Chr(34) & _
Chr(13) & Chr(13) & _
"sendmail [-help|-?]"
WSCript.Quit
End Sub
'
' Sends email from the local SMTP service using CDONTS objects
'
' Usage:
' sendmail [-smtp <server>] [-port <port>] [-auth <0=None, 1=Basic, 2=NTLM>] [-user <user>] [-pass <password>]
' -t <to> -f <from> -s "<subject>" -b "<message>" -a "<attachment1>,<attachment2>,..,<attachmentN>"
' sendmail [-help|-?]
'
'--------------------------------------------------
Option Explicit
On Error Resume Next
Dim oArgs, ArgNum
Dim strServer, intPort, intAuth, strUser, strPass, strTo, strFrom, strSubject, strBody, strAttachments
Dim arrAttachments, strAttachment
Dim strSch, objConfig, objMessage
Set oArgs = WScript.Arguments
ArgNum = 0
strServer = "localhost"
intPort = 25
intAuth = 0
While ArgNum < oArgs.Count
Select Case LCase(oArgs(ArgNum))
Case "-smtp"
ArgNum = ArgNum + 1
strServer = oArgs(ArgNum)
Case "-port"
ArgNum = ArgNum + 1
intPort = oArgs(ArgNum)
Case "-auth"
ArgNum = ArgNum + 1
intAuth = oArgs(ArgNum)
Case "-user"
ArgNum = ArgNum + 1
strUser = oArgs(ArgNum)
Case "-pass"
ArgNum = ArgNum + 1
strPass = 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 "-attachment","-a":
ArgNum = ArgNum + 1
strAttachments = oArgs(ArgNum)
Case "-help","-?":
Call DisplayUsage
Case Else:
Call DisplayUsage
End Select
ArgNum = ArgNum + 1
Wend
WScript.Echo strAttachments
If oArgs.Count=0 Or strTo="" Or strFrom="" Or _
strSubject="" Or strBody="" Then
Call DisplayUsage
Else
strSch = "http://schemas.microsoft.com/cdo/configuration/"
Set objConfig = CreateObject("CDO.Configuration")
With objConfig.Fields
.Item(strSch & "sendusing") = 2 ' cdoSendUsingPort
.Item(strSch & "smtpauthenticate") = intAuth
.Item(strSch & "smtpserver") = strServer
.Item(strSch & "smtpserverport") = intPort
If intAut = 1 Then
.Item(strSch & "sendusername") = strUser
.Item(strSch & "sendpassword") = strPass
End If
.update
End With
Set objMessage = CreateObject("CDO.Message")
With objMessage
Set .Configuration = objConfig
.From = strFrom
.To = strTo
.Subject = strSubject
.TextBody = strBody
IF strAttachments <> "" Then
arrAttachments = Split(strAttachments, ",", -1, 1)
For Each strAttachment in arrAttachments
.AddAttachment(strAttachment)
Next
Set arrAttachments = Nothing
End If
.Send
End With
Set objMessage = Nothing
Set objConfig = Nothing
End If
' Display the usage for this script
Sub DisplayUsage
WScript.Echo "Usage:" & _
Chr(13) & Chr(13) & _
"sendmail [-smtp <server>] [-port <port>] [-auth <0=None, 1=Basic, 2=NTLM>] [-user <user>] [-pass <password>]" & _
Chr(13) & _
" -t <to address> -f <from address>" & _
Chr(13) & _
" -s " & Chr(34) & "<subject>" & Chr(34) & " -b " & Chr(34) & "<message>" & Chr(34) & _
Chr(13) & _
" -a " & Chr(34) & "<attachment1>,<attachment2>,..,<attachmentN>" & Chr(34) & _
Chr(13) & Chr(13) & _
"sendmail [-help|-?]"
WSCript.Quit
End Sub

Commentaires