24h購物| | PChome| 登入
2007-11-23 01:17:44 | 人氣6,696| 回應0 | 上一篇 | 下一篇
推薦 0 收藏 0 轉貼0 訂閱站台

用bat 加上vbs 發信 不錯用

很多時候 想要讓一些工作可以自動提醒
之前我是使用vbs讓他發mail
但就太故定型式了 所以寫改了一隻比較活動式的vbs來讓大家參考
可以加在批次檔中
這樣只要修改批次檔即可工作
應網友需求 說要有可以驗證的地方
以下mb.bat為我拿下述修所部份 2006/09/25 加入
加入 HTML BODY 及 cc (寄件副本) 的部份


:: 快速夾檔寄信
:: mb.bat 所夾檔案
::mb.bat

@echo off
REM ma.bat 2006/09/25
REM 將雙引號中的變數改成你想要的即可

if "%1" == "" goto :Useage
rem s_mail所在位置 請給實際路徑 or 加到有path 的地方
set execfile="s_mail.vbs"
rem 發信者 帳號是亂打的 打打入自已的
set mailfrom=/fr "a123test@gmail.com"
rem 收件者 帳號是亂打的 打打入自已的
set mailto=/to "a123test@gmail.com"

rem 主旨
set mailsub=/sub "MEBAK Send a File"
rem 信件內容
set mailmes=/mes "Send a File %1 _ %date%%time% "

REM 驗證帳密
set user=/muser "a123test"
set password=/mpasswd "a123test"

REM smtpServer
set smtpServer=/s "smtp.gmail.com"
rem /sh 觀看 /mail 發送 smtp驗證 smtp ssl 動作 (自行參考不一定全部都有) 以下是使用 gmail 的部份
set other=/sh /mail /au /ssl /sport 465
:: 一般不用使用gmail 如下
:: set other=/sh /mail /au

REM 執行
%execfile% %smtpServer% %mailto% %mailFrom% %user% %password% %mailsub% %mailmes% %other% /add %1
Rem end
Rem ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~``
goto :End

:Useage
echo plz Give a File
echo example mb.bat "File"
goto :End

:end


----------ma.bat------------
REM ma.bat 2005'12'27
REM 將雙引號中的變數改成你想要的即可
REM 執行程式
set execfile="C:\Documents and Settings\Administrator\桌面\s_mail.vbs"
rem 發信者
set mailfrom=/fr "user@smtpmail.test.com"
rem 收件者
set mailto=/to "abc@hotmail.com;def@hotmail.com"

rem 主旨
set mailsub=/sub "mail sub aa"

rem 內文 內文可以夾文字也可以用一般的字串
REM 字串型
rem set mailmes=/mes "HI My name is CC"
rem 檔案型 在/mes 後面加上 file 加上檔名
set mailmes=/mes file "C:\Documents and Settings\Administrator\桌面\新增文字文件.txt"

REM 驗證帳密
set user=/muser "user1"
set password=/mpasswd "password"


REM 夾檔案
set add=/add "C:\Documents and Settings\Administrator\桌面\新增文字文件.txt"

REM smtpServer
set smtpServer=/s "192.168.1.10"

rem /sh 觀看 /mail 發送 smtp驗證 smtp ssl 動作 /html    加入html body 的信件  (自行參考不一定全部都有)
set other=/sh /mail /au /ssl

REM 執行
%execfile% %smtpServer% %mailto% %mailcc% %mailFrom% %user% %password% %mailsub% %add% %mailmes% %other%
Rem end
Rem ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~``


-----------S_mail.vbs---------------------
' *************************************************
' S_mail.vbs 2005'12'27
' S_mail.vbs /s 192.168.1.10 /to test@test.com.tw /cc test@test.com.tw  /fr log@MAIL /sub "testaa" /mes aaa /sh /MAIL
' edit 2006/01/04
' edit 2006/01/07
' edit 2006/01/12
' edit 2006/12/07

' *************************************************
Option Explicit

' *********************** DIM *********************
dim mailfrom,mailto,mailcc,mailsub,mailmess,MXserver,mailsmtpport,mailuser,mailpasswd
dim objEmail,msgerr
Dim stry,strm,strd,strtxt
dim fush,fuc
Dim strFlag,intState,i
Dim objFileSystem
dim FSO,objfile,ReadAllTextFile
dim add,addfile,userpd
dim strsmptaut,smptaut,htmlchk
dim strsmtpssl,smtpssl
' ***********************EDIT ***********************


' *********************** PROGRAM ***********************
call argm
CALL funchk

' ***********************SUB PROGRAM ***********************
sub argm
if Wscript.arguments.Count > 1 then
add = 0
userpd = 0
mailsmtpport = 25
for i = 0 to Wscript.arguments.Count -1
select case LCase(Wscript.arguments.Item(i))
case "/s"
MXserver = LCase(Wscript.arguments.Item(i+1))
case "/to"
mailto = LCase(Wscript.arguments.Item(i+1))
case "/cc"
mailcc = LCase(Wscript.arguments.Item(i+1))
case "/fr"
mailfrom = LCase(Wscript.arguments.Item(i+1))
case "/sub"
Mailsub = LCase(Wscript.arguments.Item(i+1))
case "/mes"
mailmess = LCase(Wscript.arguments.Item(i+1))
if mailmess = "file" then
Set FSO = CreateObject("Scripting.FileSystemObject")
Set objfile = FSO.OpenTextFile(Wscript.arguments.Item(i+2),1)
ReadAllTextFile = objfile.ReadAll
objfile.close
end if

case "/add"
add = 1
addfile = LCase(Wscript.arguments.Item(i+1))
case "/html"
htmlchk=1
case "/muser"
userpd = userpd + 1
mailuser = LCase(Wscript.arguments.Item(i+1))

case "/mpasswd"
userpd = userpd + 1
mailpasswd = Wscript.arguments.Item(i+1)

case "/au"
strsmptaut = "Yes"
smptaut = 1
case "/ssl"
strsmtpssl = "Yes"
SmtpSSL = 1

case "/sport"
mailsmtpport =int(Wscript.arguments.Item(i+1))

case "/sh"
fush = 1

case "/mail"
fuc = 1
end select
next
else
call usage
end if
end sub

sub funchk
if mailto = "" or mailfrom = "" or Mailsub = "" or mailmess = "" or MXserver = "" then
if mailfrom = "" then mailfrom = "/fr Mail From"
if mailto = "" then mailto = "/to Mail To"
if Mailsub = "" then Mailsub = "/sub Subject"
if mailmess = "" then mailmess = "/mes Message"
if MXserver = "" then MXserver = "/s Smtp Server"

call showlok
wscript.echo "You can type mal.vbs /? or mal.vbs "
wscript.quit
end if
if fush = 1 then call showlok
if fuc = 1 then call mlt
end sub

sub usage
wscript.echo ""
wscript.echo ""
wscript.echo "Executes a command."
wscript.echo ""
wscript.echo " SYNTAX: "
wscript.echo ""
wscript.echo " [/s ] "
wscript.echo " [/to ] [/cc] [/fr ]"
wscript.echo " [/sub ] [/mes | file FileName ] "
wscript.echo " [/sh ] [/mail ]"
wscript.echo " [/add ]"
wscript.echo " [/au ]"
wscript.echo " [/SSL ]"
wscript.echo " [/html ]"
wscript.quit
end sub

sub showlok
wscript.echo ""
wscript.echo "MAIL FROM : " & mailfrom
wscript.echo "MAIL TO : " & mailto
wscript.echo "MAIL SUB : " & Mailsub
wscript.echo "MAIL MESS : " & mailmess
wscript.echo "SMTP Server : " & MXserver
if smptaut = 1 then wscript.echo "SMTP Authenticate: " & strsmptaut
if smtpssl = 1 then wscript.echo "SMTP SSL: " & strSmtpSSL
if  htmlchk=1 then wscript.echo "     HtmlBody Send"
wscript.echo
end sub
'*********

sub mlt
Set objEmail = CreateObject("CDO.Message")
if mailmess = "file" then mailmess = ReadAllTextFile
with objEmail
.From = mailfrom
.To = mailto
.cc  = mailcc
.Subject = mailsub
if  htmlchk=1 then
.htmlbody=mailmess
else
.Textbody = mailmess
end if
'夾檔案
if add = 1 then
.AddAttachment addfile
end if

if userpd = 2 then
.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = mailuser
.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = mailpasswd
end if

if smptaut = 1 then
.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
end if
if smtpssl = 1 then
.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = true
end if

.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = MXserver
.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = mailsmtpport
.Configuration.Fields.Update
.Send
end with
end sub
'**********
' end

台長: Kenny
人氣(6,696) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 教育學習(進修、留學、學術研究、教育概況) | 個人分類: 程式設計 |
此分類下一篇:用 VB 禁止指定的程式 連接到外網
此分類上一篇:Show出系統修改後必須從新開機的對話方塊

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文