每30分钟执行一次vbs

时间:2012-11-06 12:47:49

标签: email batch-file vbscript

我希望outlook能够每30分钟实时向服务器发送电子邮件。有没有办法在当前的代码或批处理文件中执行此操作?使计划任务不可用且没有第三方软件

set objOutlook = CreateObject( "Outlook.Application" )
set objMail = objOutlook.CreateItem(0)

strMessage = "Test"

ON ERROR RESUME NEXT
With objMail
    .From = "email"
    .To = "email"
    .Subject = "Test"
    .Body = strMessage
    .Save
end with

objMail.OriginatorDeliveryReportRequested = True
objMail.Display
objMail.Send

WScript.quit

2 个答案:

答案 0 :(得分:3)

使用VBScript睡眠的一个例子:

Dim waittime : waittime = 30 * 60 * 1000    

do 

    ' Insert your code here

    WScript.Sleep(waittime)
loop

哦,摆脱WScript.Quit语句,那将......退出你的脚本!

修改

另一种方法整合邮箱是使用WshShell.Popup

do

    ' Insert your code here

    Set WshShell = CreateObject("WScript.Shell")
    If WshShell.Popup ( "Sending a mail every 30 minutes" & vbNewLine & _
                        "Press Cancel to stop or OK to send a mail right now.", _
                            waittime, "Automatic mail sender", 1 or 32 or 4096) = 2  Then
        exit do
    end if

loop

此代码段将每隔waittime执行一次代码,当按下“取消”按钮时它会停止,并且会在按下OK时立即执行代码。

答案 1 :(得分:1)

在vbs(首选)中使用while true循环,或在批处理文件中使用循环(脏路)。

:back
myscript.vbs
ping -n 1800 localhost >nul 2>nul 
REM ping -n is a dirty way of sleeping for system <= winxp
REM for win7+ systems, use command `timeout 1800`
goto back

Wscript方法:

while true
    set objOutlook = CreateObject( "Outlook.Application" )
    set objMail = objOutlook.CreateItem(0)

    strMessage = "Test"

    ON ERROR RESUME NEXT
    With objMail
        .From = "email"
        .To = "email"
        .Subject = "Test"
        .Body = strMessage
        .Save
    end with

    objMail.OriginatorDeliveryReportRequested = True
    objMail.Display
    objMail.Send
    wscript.sleep(30*60*1000)
wend