2016-10-10 64 views
-1

我使用以下脚本在白天发送一些电子邮件,它需要一个或多个参数(有几个版本),并由.bat文件调用。该脚本是:VBscript - 文本文件正文到电子邮件

Const schema = "http://schemas.microsoft.com/cdo/configuration/" 
Const cdoBasic = 2 
Const cdoSendUsingPort = 2 
Dim oMsg, oConf 
Dim sDateTimeStamp 

Set args = WScript.Arguments 
arg1 = args(0) 


' E-mail properties 
Set oMsg  = CreateObject("CDO.Message") 
oMsg.From  = "[email protected]" ' or "Sender Name <[email protected]>" 
oMsg.To  = "[email protected]" ' or "Recipient Name <[email protected]>" 
oMsg.Subject = "System Message" 
oMsg.BodyPart.Charset = "Windows-1253" 
oMsg.Textbody = "Attached files." & vbcrlf & _ 
    "This on a new line" & vbcrlf & _ 
    "This on yet another" 


Const ForReading = 1 
Const ForWriting = 2 
Const ForAppending = 8 

Const FileToBeUsed = "DIRTEST.TXT" 
Dim fso, f, g 
Set fso = CreateObject("Scripting.FileSystemObject") 

Set f = fso.OpenTextFile(FileToBeUsed, ForReading) 

g = f.ReadAll 

f.Close 

Set f = Nothing 
Set fso = Nothing 



' GMail SMTP server configuration and authentication info 
Set oConf = oMsg.Configuration 
oConf.Fields(schema & "smtpserver")  = "gmail.com" 'server address 
oConf.Fields(schema & "smtpserverport") = 587    'port number 
oConf.Fields(schema & "sendusing")  = cdoSendUsingPort 
oConf.Fields(schema & "smtpauthenticate") = cdoBasic   'authentication type 
oConf.Fields(schema & "smtpusessl")  = False    'use SSL encryption 
oConf.Fields(schema & "sendusername")  = "[email protected]" 'sender username 
oConf.Fields(schema & "sendpassword")  = "XXXXXX"  'sender password 
oConf.Fields.Update() 

'base64 

' send message 
oMsg.Send() 

' Return status message 
If Err Then 
    resultMessage = "ERROR " & Err.Number & ": " & Err.Description 
    Err.Clear() 
Else 
    resultMessage = "Success Notification Message sent succesfully." 
End If 

Wscript.echo(resultMessage) 

现在文本主体设置为:

Attached Files 
    This is a new line 
    This is yet another 

我想插线1和2之间的目录列表,直接或通过保存列出目录一个文本文件,然后把所述文件的内容在电子邮件正文中,像这样:

Attached Files 
    06/10/2016 <TIME>   13.000 Name1.txt 
    06/10/2016 <TIME>   300.000 Name2.pdf 
    06/10/2016 <TIME>   150.000 Name3.pdf 
    06/10/2016 <TIME>   5.000.000 Name4.pdf 
    This is a new line 
    This is yet another 

编辑:上面的代码成功地追加目录列表,邮件主题,而且还附加了一个BATC h的乱码字符在顶部。

+0

所以你有没有尝试过放置目录列表,代码在哪里呢?......我们不需要看到所有这些。请参阅提供[mcve]。 – Lankymart

+0

如果我知道如何去做,我显然不需要问。 – onlyf

+0

虽然我问你是否有*“尝试”*,但还是有区别的。理想情况下,需要表明*提问者努力解决问题的努力程度,并伴随[mcve]显示他们到达的具体问题以及他们遇到的具体问题*(错误消息等)*。目前这既不满足,也可能会被视为“太广泛”。 – Lankymart

回答

0

该脚本不言自明

编辑:合成尺寸。还要注意它给出了文件夹的大小。这可能很慢,您可能想要省略文件夹。例如,第一次运行上面的代码(在c:\文件夹中)时,窗口必须将每个文件夹读入内存。这需要一段时间。第二次运行它时,所有文件夹都将位于磁盘缓存中,并且速度会非常快。

Edit2 VBS帮助文件最近在MS网站上被删除。它在我的skydrive上可用https://1drv.ms/f/s!AvqkaKIXzvDieQFjUcKneSZhDjw它被称为script56.chm。

Set fso = CreateObject("Scripting.FileSystemObject") 


    On Error Resume Next 
    Set fldr = fso.GetFolder("c:\") 

    Set Fls = fldr.files 
    Set Fldrs = fldr.subfolders 
    For Each thing in Fls 
     A= A & vbtab & thing.name & vbtab & thing.attributes & vbtab & FormatNumber(thing.size, 0) & vbtab & Thing.DateLastModified & vbcrlf 
    Next 
    For Each thing in Fldrs 
     A= A & vbtab & thing.name & vbtab & thing.attributes & vbtab & FormatNumber(thing.size, 0) & vbtab & Thing.DateLastModified & vbcrlf 
    Next 

     msgbox a 


      msgbox a