使用VB脚本发送邮件?

时间:2013-03-23 11:22:20

标签: scripting vbscript wmi wmi-query

我有以下代码来监控硬盘。现在我为每个文件创建或删除事件获取Echo。

是否存在修改WScript.Echo以发送邮件通知的方法?

strDrive = "c"
arrFolders(0) = strDrive & "\\\\"
 strComputer = "." 
 Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") 
 'Loop throught the array of folders setting up the monitor for Each 
 i = 0 
 For Each strFolder In arrFolders 
     'Create the event sink 
     strCommand = "Set EventSink" & i & " = WScript.CreateObject" & "(""WbemScripting.SWbemSink"", ""SINK" & i & "_"")" 
     ExecuteGlobal strCommand 
     'Setup Notification 
     strQuery = "SELECT * FROM __InstanceOperationEvent WITHIN 1 " & "WHERE Targetinstance ISA 'CIM_DirectoryContainsFile'" & " and TargetInstance.GroupComponent = " & "'Win32_Directory.Name=""" & strFolder & """'"
     strCommand = "objWMIservice.ExecNotificationQueryAsync EventSink" & i & ", strQuery"
     ExecuteGlobal strCommand 
     'Create the OnObjectReady Sub 
     strCommand = "Sub SINK" & i & "_OnObjectReady(objObject, " &  "objAsyncContext)" & VbCrLf & vbTab & "Wscript.Echo objObject.TargetInstance.PartComponent" & VbCrLf & "End Sub"
     WScript.Echo strCommand 
     ExecuteGlobal strCommand 
     i = i + 1 
 Next 
 WScript.Echo "Waiting for events..." 

 i = 0 
 While (True) 
     Wscript.Sleep(1000) 
 Wend

而不是像下面那样回应:

strCommand = "Sub SINK" & i & "_OnObjectReady(objObject, " &  "objAsyncContext)" & VbCrLf & vbTab & "Wscript.Echo objObject.TargetInstance.PartComponent" & VbCrLf & "End Sub"

我想发送这样的邮件:

strCommand = "Sub SINK" & i & "_OnObjectReady(objObject, " &  "objAsyncContext)" & VbCrLf & vbTab & "

Set outobj = CreateObject("Outlook.Application")
    Set mailobj = outobj.CreateItem(0)
    With mailobj
        .To = toAddress
        .Subject = Subject
        .HTMLBody = strHTML
        .Send
    End With

" & VbCrLf & "End Sub"

是否有可能或有其他方法可以做到这一点..?

2 个答案:

答案 0 :(得分:4)

我不知道您使用的服务器,但在Windows 2003和2008上,例如您可以使用CDO对象来创建电子邮件。您可以使用智能主机将电子邮件发送到。

点击此链接:http://www.paulsadowski.com/wsh/cdo.htm

此外,您可以选择任何免费的电子邮件组件来创建电子邮件并使用smtp服务器发送您的电子邮件。或者在这一侧查看可以使用组件的部分,其中包括许多示例:http://www.chilkatsoft.com/email-activex.asp

**更新**

此脚本会根据您的要求检查并发送电子邮件:

strDrive = "d:"
Dim arrFolders(0) : arrFolders(0) = strDrive & "\\\\"
strComputer = "." 
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") 
'Loop throught the array of folders setting up the monitor for Each 
i = 0 
For Each strFolder In arrFolders 
  'Create the event sink 
  WScript.Echo "setup for folder: " & strFolder & vbLf
  strCommand = "Set EventSink" & i & " = WScript.CreateObject" & "(""WbemScripting.SWbemSink"", ""SINK" & i & "_"")" 
  ExecuteGlobal strCommand
  'Setup Notification 
  strQuery = "SELECT * " _
          & "FROM __InstanceOperationEvent " _
          & "WITHIN 1 " _
          & "WHERE Targetinstance ISA 'CIM_DirectoryContainsFile'" _
          & "  AND TargetInstance.GroupComponent = " & "'Win32_Directory.Name=""" & strFolder & """'"
  strCommand = "objWMIservice.ExecNotificationQueryAsync EventSink" & i & ", strQuery"
  ExecuteGlobal strCommand 
  'Create the OnObjectReady Sub 
  strCommand = "Sub SINK" & i & "_OnObjectReady(objObject, " &  "objAsyncContext)" & vbLf _
            & "  Wscript.Echo objObject.TargetInstance.PartComponent" & vbLf _
            & "  SendMail(objObject.TargetInstance.PartComponent)" & vbLf _
            & "End Sub"
  'WScript.Echo strCommand 
  ExecuteGlobal strCommand 
  i = i + 1 
Next 

WScript.Echo "Waiting for events..." 
i = 0 
While (True) 
  Wscript.Sleep(1000) 
Wend

Function SendMail(vBody)

  Dim oMail : Set oMail = CreateObject("CDO.Message")

  'Name or IP of Remote SMTP Server
  oMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 
  oMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "your.smtp.server"
  oMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 
  oMail.Configuration.Fields.Update

  oMail.Subject = "Email Watch Info Message"
  oMail.From = "alert@yourdomain.net"
  oMail.To = "target@yourdomain.net"
  oMail.TextBody = vBody
  oMail.Send

End Function

更正发送邮件功能中的设置即可。

答案 1 :(得分:1)

理论上,VBSendMail DLL应该能够做你想要的。