上传文件后发送自动发送电子邮件?

时间:2013-05-01 14:24:01

标签: vbscript batch-file

有两个文件每天凌晨1点到凌晨1点15分在D:\ CW-Data \ edw位置上传。 我的要求是在收到两个文件时自动向我们发送带有文件名的电子邮件。 我尝试写一个cript,它通过了到达的lastfile,但我没有得到预期的结果。请帮帮我: 我的剧本是

set srcDir=D:\Mitul\Quantum AWR Report_23Apr_3am_2pm
set lastmod=
pushd %srcDir%
for /f "tokens=*" %%a in ('dir /b /od 2^>NUL') do set lastmod=%%a
echo %lastmod%

2 个答案:

答案 0 :(得分:1)

这是用于发送电子邮件的通用脚本。

https://groups.google.com/forum/?fromgroups=#!msg/alt.msdos.batch.nt/l_8K11YzS0A/WfbVBoJe-l8J

的原始帖子
:: Allows ssl and port 465



:: email.cmd ::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Tom Lavedas, Mod 2.14 to eliminate temporary VBS script file
:: Works with IE5+ (and maybe earlier versions as well)
 @echo off
  setlocal

 :Use command line arguments, if supplied
  if [%1] EQU [] goto :Continue
    set "Arg=%1"
    set "Arg=%Arg::==%"
    set "Arg=%Arg:/=%"
    set "%Arg%"
    shift
    goto :Use 
 :Continue

:: defaults 
  if not defined From       set "From=userid@gmail.com"
  if not defined To         set "To=you@somemail.com"
  if not defined Subj       set Subj="email test   %date% %time%"
  if not defined Body       set Body="Did it work? %date% %time%"
  if not defined Serv       set "Serv=smtp.gmail.com"
  if not defined Auth       set "Auth=userid@gmail.com"
  if not defined Pass       set "Pass=password"
  if not defined fileattach set "fileattach="
  if not defined Port       set "Port=465"
  if not defined SSL        set "SSL=True"
  if not defined Timeout    set "Timeout=25"

  call :send %From% %To% %Subj% %Body% %Serv% %Auth% %Pass% %fileattach%
  if %errorlevel% NEQ 0 echo Error: %ErrorLevel%
  if %0==%~f0 pause
  exit /b %ErrorLevel%

 :send

  set "cdoSchema=http://schemas.microsoft.com/cdo/configuration"
  set arguments="%~1+%~2+%~3+%~4+%~5+%~6+%~7+%~f8"

  set VBS=resizeTo 1,1:moveTo 1, 3000:
  set VBS=%VBS%set ws=createobject("wscript.shell"):
  set VBS=%VBS%with createobject("scripting.filesystemobject")
  set VBS=%VBS%Execute ws.ExpandEnvironmentStrings(
  set VBS=%VBS%.GetStandardStream(0).readALL):end with:
  set "VBS=about:<script type=text/vbs>%VBS%:close</script>"

  set "Match=VBScript start"
  for /f "delims=[]" %%N in (
    'find /n "### %Match% ###" ^<"%~f0"'
  ) do set "N=%%N"

  for /f "tokens=1,2 delims=#" %%A in (
    'more +%N% "%~f0" ^| mshta.exe "%VBS%"'
  ) do @echo %%B & exit /b %%A
  exit /b 1 % Error in previous statement %

' ### VBScript start ###
 set StdOut=.GetStandardStream(1)
 args = Split(%arguments%, "+")
 with CreateObject("CDO.Message")
   .From     = args(0)
   .To       = args(1)
   .Subject  = args(2)
   .Textbody = args(3)
  if args(7) <> "" then .AddAttachment args(7)
   with .Configuration.Fields
     .Item ("%cdoSchema%/sendusing")        = 2 ' not local, smtp
     .Item ("%cdoSchema%/smtpserver")       = args(4)
     .Item ("%cdoSchema%/smtpserverport")   = %Port%
     .Item ("%cdoSchema%/smtpauthenticate") = 1 ' cdobasic
     .Item ("%cdoSchema%/sendusername")     = args(5)
     .Item ("%cdoSchema%/sendpassword")     = args(6)
     .Item ("%cdoSchema%/smtpusessl")       = %SSL%
     .Item ("%cdoSchema%/smtpconnectiontimeout") = %Timeout%
     .Update 
   end with ' Configuration.Fields
   On Error Resume Next
   .Send
 end with ' CDO.Message
 if Err.Number = 0 then
   sRes = "0#Mail sent without error"
 else
   sRes = Hex(Err.Number) + "#" + Err.Description
 end if
 stdout.writeline sRes
' ### VBScript end ### - Must be the end of the batch file

答案 1 :(得分:0)

@echo off
setlocal
set srcDir=D:\CW-Data\edw
set file1=whateverthenameofthefirstfileis
set file2=whateverthenameofthesecondfileis
:loop
set /a count=0
for /f %%a in ('dir /b "%srcdir%\%file1%" "%srcdir%\%file2%" 2^>NUL') do set /a count+=1
IF NOT %count%==2 timeout /t 5 >nul&GOTO loop 
echo send the email

这将等待两个文件到达。如果其中任何一个丢失,请超时5秒,然后重试。

没有足够的细节来提供更多信息。

<小时/> 附录:

如果您的Windows版本不包含timeout,请尝试

IF NOT %count%==2 ping -n 5 127.0.0.1 >nul&GOTO loop