0

当用户将文件上传到文件夹时,我必须立即向所有用户发送电子邮件。我现有的代码工作如下:我们有任务计划程序,它将在每天上午 11 点运行,文件将过滤修改日期。我的目标是在用户上传文件但无法运行调度程序 1 小时或每 5 分钟时发送电子邮件,因为修改日期是文件创建日期而不是上传日期。在下面的文件夹中是代码,任何人都可以帮助我更新代码,以便用户可以立即收到。

Dim objFso
Set objFso = CreateObject("Scripting.FileSystemObject")

Dim strPath, yr, mnt
yr  = CStr(Year(Now))
mnt = CStr(Month(Now))
strPath = "c:\\users\upload files\email " + yr

Dim strContent 
strContent = ""
Set objFolder = objFso.GetFolder(strPath)

For Each objFile In objFolder.Files
    If objFso.GetExtensionName (objFile.Path) = "pdf" Then
        If objFile.DateLastModified > dateadd("hr", -24, Now) Then
            strContent = strContent + "<li>" + _
                "<a href=""C://Users/uploadfiles/email/" + yr + "/" + _
                objFile.Name + """>" + objFile.Name + "</a></li>"
            MsgBox(strContent)
        End If
    End If
4

1 回答 1

1
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
("SELECT * FROM __InstanceCreationEvent WITHIN 10 WHERE Targetinstance ISA 'CIM_DirectoryContainsFile' and TargetInstance.GroupComponent= 'Win32_Directory.Name=""c:\\\\scripts""'")
Do
    Set objLatestEvent = colMonitoredEvents.NextEvent
    Wscript.Echo objLatestEvent.TargetInstance.PartComponent
Loop

这将监视 c:\scripts (注意反斜杠必须加倍然后再加倍)以查看每 10 秒创建的文件(within 10)。

Set emailObj      = CreateObject("CDO.Message")
emailObj.From     = "dc@gmail.com"

emailObj.To       = "dc@gmail.com"

emailObj.Subject  = "Test CDO"
emailObj.TextBody = "Test CDO"

emailObj.AddAttachment "c:\windows\win.ini"

Set emailConfig = emailObj.Configuration

emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing")    = 2  
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1  
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpusessl")      = true 
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendusername")    = "Username"
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendpassword")    = "Password"
emailConfig.Fields.Update

emailObj.Send

If err.number = 0 then Msgbox "Done"
于 2016-06-20T12:46:41.403 回答