2015年11月10日 星期二

批次 用vbs發信很簡單

轉自  http://adalf0722.blogspot.tw/2008/12/vbs.html

用vbs發信很簡單

server上不需安裝smtp服務,但你必須有一發信帳戶就可以使用這個發信囉,發信時還可附加檔案或從文字檔讀取內容喔

1.首先將下列script存成mail.vbs
Option Explicit
'變數宣告
dim FSO,objfile
dim SMTPSVR,MailFrom,MailTo
dim User,Password,Subject
dim Content,Nikename
dim addFile
'取參數
SMTPSVR = Wscript.arguments.Item(0)
User = Wscript.arguments.Item(1)
Password = Wscript.arguments.Item(2)
MailFrom = Wscript.arguments.Item(3)
MailTo = Wscript.arguments.Item(4)
Subject = Wscript.arguments.Item(5)
Content = Wscript.arguments.Item(6)
Nikename = Wscript.arguments.Item(7)
addFile = Wscript.arguments.Item(8)

MailFrom = """" & Nikename & """" & MailFrom

Call sendMail(SMTPSVR,User,Password,MailFrom,MailTo,Subject,Content,addFile)

Sub sendMail(SMTPSVR,User,Password,MailFrom,MailTo,Subject,Body,addFile)
Const cdoSendUsingPickup = 1 'Send message using the local SMTP service pickup directory.
Const cdoSendUsingPort = 2 'Send the message using the network (SMTP over the network).
Const cdoAnonymous = 0 'Do not authenticate
Const cdoBasic = 1 'basic (clear-text) authentication
Const cdoNTLM = 2 'NTLM
dim objMessage
dim ReadAllTextFile
Set objMessage = CreateObject("CDO.Message")
objMessage.Subject = Subject
objMessage.From = MailFrom
objMessage.Sender = MailFrom
objMessage.To = MailTo

'讀取文字檔
Set FSO = CreateObject("Scripting.FileSystemObject")
If FSO.FileExists(Body) Then
Set objfile = FSO.OpenTextFile(Body,1)
ReadAllTextFile = objfile.ReadAll
objfile.close
else
ReadAllTextFile = "文字檔案不存在!"
end if

if addFile <> "" then
If FSO.FileExists(addfile) Then
objMessage.AddAttachment addfile
else
ReadAllTextFile = ReadAllTextFile & " 附加檔案不存在!"
end if

end if
objMessage.TextBody = ReadAllTextFile

'objMessage.AddAttachment strAddfile
'==This section provides the configuration information for the remote SMTP server.
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
'Name or IP of Remote SMTP Server
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = SMTPSVR
'Type of authentication, NONE, Basic (Base64 encoded), NTLM
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = cdoBasic
'Your UserID on the SMTP server
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusername") = User
'Your password on the SMTP server
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendpassword") = Password
'Server port (typically 25)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
'Use SSL for the connection (False or True)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = False
'Connection Timeout in seconds (the maximum time CDO will try to establish a connection to the SMTP server)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
objMessage.Configuration.Fields.Update
'==End remote SMTP server configuration section==
objMessage.Send
Set objMessage = Nothing
'wscript.echo "信件已寄出"
End Sub
2.再將下列命令存成content.bat ,內容視情況修改("::"後面接的是註解喔)
::vbs的位置
set mailApp=".\mail.vbs"
::smtp server
set SMTPSVR="xxx.xxx.xxx"
::username
set User="username"
::password
set Password="pw"
::寄件者(暱稱,email address)
set MailFrom="test@mail.com.xx"
::收件者
set MailTo="test@mail.com.xx"
::主旨
set Subject="主旨"
::內容檔案的位置
set Content="d:\cc.txt"
::
set Nickname="管理者"
::附加檔案的位置
set addFile="d:\cc.txt"
::執行指令
call %mailApp% %SMTPSVR% %User% %Password% %MailFrom% %MailTo% %Subject% %Content% %Nickname% %addFile%
::pause

3.mail.vbs 與content.bat請放在同一層,執行 content.bat就可以發信囉

沒有留言:

張貼留言