这是用于发送电子邮件的通用脚本。
https://groups.google.com/forum/?fromgroups=#!msg/alt.msdos.batch.nt/l_8K11YzS0A/WfbVBoJe-l8J的原始线程
:: Allows ssl and port 465
:: email.cmd ::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Tom Lavedas, Mod 2.14 to eliminate temporary VBS script file
:: Works with IE5+ (and maybe earlier versions as well)
@echo off
setlocal
:Use command line arguments, if supplied
if [%1] EQU [] goto :Continue
set "Arg=%1"
set "Arg=%Arg::==%"
set "Arg=%Arg:/=%"
set "%Arg%"
shift
goto :Use
:Continue
:: defaults
if not defined From set "From=userid@gmail.com"
if not defined To set "To=you@somemail.com"
if not defined Subj set Subj="email test %date% %time%"
if not defined Body set Body="Did it work? %date% %time%"
if not defined Serv set "Serv=smtp.gmail.com"
if not defined Auth set "Auth=userid@gmail.com"
if not defined Pass set "Pass=password"
if not defined fileattach set "fileattach="
if not defined Port set "Port=465"
if not defined SSL set "SSL=True"
if not defined Timeout set "Timeout=25"
call :send %From% %To% %Subj% %Body% %Serv% %Auth% %Pass% %fileattach%
if %errorlevel% NEQ 0 echo Error: %ErrorLevel%
if %0==%~f0 pause
exit /b %ErrorLevel%
:send
set "cdoSchema=http://schemas.microsoft.com/cdo/configuration"
set arguments="%~1+%~2+%~3+%~4+%~5+%~6+%~7+%~f8"
set VBS=resizeTo 1,1:moveTo 1, 3000:
set VBS=%VBS%set ws=createobject("wscript.shell"):
set VBS=%VBS%with createobject("scripting.filesystemobject")
set VBS=%VBS%Execute ws.ExpandEnvironmentStrings(
set VBS=%VBS%.GetStandardStream(0).readALL):end with:
set "VBS=about:<script type=text/vbs>%VBS%:close</script>"
set "Match=VBScript start"
for /f "delims=[]" %%N in (
'find /n "### %Match% ###" ^<"%~f0"'
) do set "N=%%N"
for /f "tokens=1,2 delims=#" %%A in (
'more +%N% "%~f0" ^| mshta.exe "%VBS%"'
) do @echo %%B & exit /b %%A
exit /b 1 % Error in previous statement %
' ### VBScript start ###
set StdOut=.GetStandardStream(1)
args = Split(%arguments%, "+")
with CreateObject("CDO.Message")
.From = args(0)
.To = args(1)
.Subject = args(2)
.Textbody = args(3)
if args(7) <> "" then .AddAttachment args(7)
with .Configuration.Fields
.Item ("%cdoSchema%/sendusing") = 2 ' not local, smtp
.Item ("%cdoSchema%/smtpserver") = args(4)
.Item ("%cdoSchema%/smtpserverport") = %Port%
.Item ("%cdoSchema%/smtpauthenticate") = 1 ' cdobasic
.Item ("%cdoSchema%/sendusername") = args(5)
.Item ("%cdoSchema%/sendpassword") = args(6)
.Item ("%cdoSchema%/smtpusessl") = %SSL%
.Item ("%cdoSchema%/smtpconnectiontimeout") = %Timeout%
.Update
end with ' Configuration.Fields
On Error Resume Next
.Send
end with ' CDO.Message
if Err.Number = 0 then
sRes = "0#Mail sent without error"
else
sRes = Hex(Err.Number) + "#" + Err.Description
end if
stdout.writeline sRes
' ### VBScript end ### - Must be the end of the batch file