2013-03-13 87 views
0

我想批量发送电子邮件,但由于mailto命令不会通过outlook发送电子邮件,而不需要我单击发送按钮。如何在批处理脚本中调用或嵌入VBS

我发现这个VBS脚本在线发送电子邮件没有人为交互。

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

' Create email object 
Set oolApp = CreateObject("Outlook.Application") 
Set email = oolApp.CreateItem(0) 
email.Recipients.Add("[email protected]") 

' 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 
+0

http://stackoverflow.com/questions/9074476/is-it-possible-to-embed-and-execute-vbscript-within-a-batch-file-without-using-a/26922679#26922679 – 2014-11-14 04:04:57

回答

2

编辑:现在,我重新阅读你的问题,我不完全知道你是问什么。你问如何运行一个VBScript?只要将它保存到filename.vbs和(如果你想避免显示微软的CSCRIPT虚荣心垃圾邮件或cscript /nologo filename.vbs)与

cscript filename.vbs 

执行它。


在另一方面,如果你想将其纳入一个批处理脚本这一点,那么有很多的回响从批处理脚本内容到外部文件的方法。大多数人只是做类似

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 [email protected] 

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); 

我没有测试过这一点,但玩它,看看你的想法。

+0

嗨,你还记得你帮忙写:http://stackoverflow.com/questions/15364653/can-we-fetch-scrape-particular-data-from-html-site-into-batch-to-do-following/15368190#15368190昨天?所以我试图让蝙蝠脚本通过outlook发送给我电子邮件,如果它自上次更新以来已经30分钟了。谢谢 – Mowgli 2013-03-13 14:54:45

+0

你能告诉我什么是'goto:EOF'下面的代码吗?谢谢 – Mowgli 2013-03-13 15:08:57

+1

JScript的东西是擦掉'wget'的输出。它去掉html标签,修剪空白,并将' '转换为空格,以便到达'3月11日11:14 pm最后更新'文本。然后它将该位文本转换为Date对象,从当前日期中减去它,并回显毫秒差异的数量。该差异被捕获为脚本顶部附近的'%% x'。它与[昨天](http://stackoverflow.com/a/15368190/1683264)相同。 – rojo 2013-03-13 15:12:03