如何在批处理脚本中调用或嵌入VBS

时间:2013-03-13 14:11:55

标签: vbscript batch-file batch-processing

我想批量发送电子邮件,但由于mailto命令不会通过outlook发送电子邮件而不点击发送按钮。

我在网上发现了这个VBS脚本,无需人工干预即可发送电子邮件。

我只需要帮助电话,或者我可以将VBS嵌入到BATCH文件中。

' Create email object
Set oolApp = CreateObject("Outlook.Application")
Set email = oolApp.CreateItem(0)
email.Recipients.Add("myemail@esomeemail.com")

' Create the body of the email
MailBody = "<!DOCTYPE HTML PUBLIC ""-//W3C//DTD W3 HTML//EN"">"
MailBody = MailBody & "<HTML>" & vbcrlf
MailBody = MailBody & "<HEAD><TITLE>No Invoices</TITLE></HEAD>"
MailBody = MailBody & "<BODY>" & vbcrlf
MailBody = MailBody & "<B>For Your Information</B>,<BR><BR>"
MailBody = MailBody & "No invoices were issued today.<BR><BR>"
MailBody = MailBody & "</BODY></HTML>"

' Send the Email
email.Subject = "No Invoices Issued"
email.HTMLBody = MailBody
email.Send

1 个答案:

答案 0 :(得分:2)

编辑:现在我重新阅读了您的问题,我不确定您在问什么。你在问如何运行vbscript吗?只需将其保存到filename.vbs并使用

执行即可
cscript filename.vbs

(或cscript /nologo filename.vbs如果你想避免显示微软的cscript虚荣垃圾邮件。)


另一方面,如果要将其合并到批处理脚本中,则有许多方法可以将内容从批处理脚本回显到外部文件。大多数人只是做类似

的事情
echo Set oolApp = CreateObject^("Outlook.Application"^)>> vbsfile

或类似,必要时使用^进行转义。但是,您可能会发现this page of heredoc methods很有用。以下是昨天使用script I helped you make的示例:

@if (@X)==(@Y) @end /* (batch + jscript hybrid script init)

:: *** Batch script *****

@echo off
setlocal enabledelayedexpansion

set recipient=myemail@esomeemail.com

for /f "delims=" %%I in ('wget "%~1" -O- -q 2^>NUL ^| findstr /i "last.*updated.*as.*of"') do (
    for /f "delims=" %%x in ('cscript /nologo /e:jscript "%~f0" "%%I"') do (
        set /a "thirtyMinutes = 30 * 60 * 1000"
        if %%x GEQ !thirtyMinutes! (
            call :doEmail
        )
    )
    exit /b
)
exit /b

:doEmail
call :heredoc vbs >email.vbs && goto endvbs
' Create email object
Set oolApp = CreateObject("Outlook.Application")
Set email = oolApp.CreateItem(0)
email.Recipients.Add("!recipient!")

' Create the body of the email
MailBody = "<^!DOCTYPE HTML PUBLIC ""-//W3C//DTD W3 HTML//EN"">"
MailBody = MailBody & "<HTML>" & vbcrlf
MailBody = MailBody & "<HEAD><TITLE>No Invoices</TITLE></HEAD>"
MailBody = MailBody & "<BODY>" & vbcrlf
MailBody = MailBody & "<B>For Your Information</B>,<BR><BR>"
MailBody = MailBody & "No invoices were issued today.<BR><BR>"
MailBody = MailBody & "</BODY></HTML>"

' Send the Email
email.Subject = "No Invoices Issued"
email.HTMLBody = MailBody
email.Send
:endvbs

cscript /nologo email.vbs
del email.vbs
goto :EOF

:: https://stackoverflow.com/a/15032476/1683264
:heredoc <uniqueIDX>
setlocal enabledelayedexpansion
set go=
for /f "delims=" %%A in ('findstr /n "^" "%~f0"') do (
    set "line=%%A" && set "line=!line:*:=!"
    if defined go (if #!line:~1!==#!go::=! (goto :EOF) else echo(!line!)
    if "!line:~0,13!"=="call :heredoc" (
        for /f "tokens=3 delims=>^ " %%i in ("!line!") do (
            if #%%i==#%1 (
                for /f "tokens=2 delims=&" %%I in ("!line!") do (
                    for /f "tokens=2" %%x in ("%%I") do set "go=%%x"
                )
            )
        )
    )
)
goto :EOF

:: *** JScript script *****/
var args = [];
for (var i=0; i<WScript.arguments.length; i++) { args.push(WScript.arguments(i)) }
var t = args.join(' ').replace(/^\s+|<[^>]+>|\s+$/g,'').replace(/\&nbsp;/g, ' ').split(' ');
var h = t[4].split(':')[0];
if (/pm/i.test(t[5])) h = h * 1 + 12;
var ds = t[6] + ' ' + t[7] + ', ' + new Date().getFullYear() + ' ' + h + ':' + t[4].split(':')[1];
var diff = new Date() - new Date(ds);
WScript.echo(diff);

我没有对此进行过测试,但是要玩弄它,看看你的想法。