VBscript - 将文本文件正文转换为电子邮件

时间:2016-10-10 11:19:58

标签: email text vbscript

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

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     = "myemail@gmail.com"  ' or "Sender Name <from@gmail.com>"
oMsg.To       = "otheremail@gmail.com"    ' or "Recipient Name <to@gmail.com>"
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")     = "mymy@gmail.com" '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

编辑:上面的代码成功地将dir列表附加到邮件主题,但也在顶部附加了一批乱码。

1 个答案:

答案 0 :(得分:0)

脚本是自解释的

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

Edit2 VBS帮助文件最近已在MS网站上删除。它可以在https://1drv.ms/f/s!AvqkaKIXzvDieQFjUcKneSZhDjw的我的skydrive上找到它叫做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
相关问题