0

我有两个文本文件:

c:\file1.txt
c:\file2.txt

我想使用 VBScript 将两个文件的内容作为正文发送到一封电子邮件中。我正在尝试使用下面的代码,但它不起作用。

Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Const FileToBeUsed = "c:\file1.txt"
Const FileToBeUsed = "c:\file2.txt"
Dim objCDO1
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile(FileToBeUsed, ForReading)
Set objCDO1 = CreateObject("CDO.Message")
objCDO1.Textbody = f.ReadAll
f.Close
objCDO1.TO ="sunny@abc.com"
objCDO1.From = "dontreply@abc.com (CCP Stored Procedure Message)"
objCDO1.Subject = "CCP Stored Procedure"
objCDO1.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration   /sendusing") = 2 
objCDO1.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtpb.intra.abc.com"
objCDO1.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration /smtpserverport") = 25 
objCDO1.Configuration.Fields.Update     
objCDO1.Send
Set f = Nothing
Set fso = Nothing

请帮帮我。

编辑1

当我将上面的代码修改为:

Const FileToBeUsed = "c:\file1.txt"
Const FileToBeUsed = "c:\file2.txt"
--------------------------
Set f = fso.OpenTextFile(FileToBeUsed1, ForReading) + fso.OpenTextFile(FileToBeUsed2, ForReading)
-----------------------------

objCDO1.Textbody = fso.OpenTextFile(FileToBeUsed1, ForReading).ReadAll + fso.OpenTextFile(FileToBeUsed2, ForReading).ReadAll

它在第 9 行抛出运行时错误:

对象不支持此属性或方法。

编辑2

我有一个文本文件:

输出.txt:

OPERATING SYSTEM       SERVER1    SERVER2
Windows                  1.36       4.42
Linux                    2.78       5.76
MacOS                    3.45       6.39
Ubuntu                   4.12       0.00
Android                  0.00       3.46
FreePhysicalMemory      30.12      31.65
TotalVisibleMemorySize  48.00      48.00

我想将 Output.txt 的内容作为正文发送到电子邮件中,以使其格式(对齐)不会改变(如 HTMIL 表格式):

我如何以 HTML 表格的形式将 Output.txt 文件的内容附加到电子邮件正文..?

编辑3

现在我创建了以下代码:

Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Dim objEmail, i
Set objEmail = CreateObject("CDO.Message")
objEmail.Textbody = myTextBody
objEmail.HTMLBody = myHTMLBody
If IsArray( myAttachment ) Then
For i = 0 To UBound( "c:\output.txt" )
.AddAttachment Replace( "c:\output.txt" ( i ), "" ),"",""
 Next
ElseIf myAttachment <> "" Then
.AddAttachment Replace( "c:\output.txt", ""),"",""
End If
objEmail.TO ="sunny@abc.com"
objEmail.From = "dontreply@abc.com (CCP Stored Procedure Message)"
objEmail.Subject = "CCP Stored Procedure"
objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration    /sendusing") = 2 
objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtpb.intra.abc.com"
objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration /smtpserverport") = 25 
objEmail.Configuration.Fields.Update     
objEmail.Send
Set objEmail = Nothing

邮件已发送,但邮件正文一无所获..上面的错误是什么?

4

1 回答 1

1

如果这是一个常数

Const FileToBeUsed = "c:\file1.txt"

它如何更改为另一个值?

Const FileToBeUsed = "c:\file2.txt"

尝试

Const FileToBeUsed1 = "c:\file1.txt"
Const FileToBeUsed2 = "c:\file2.txt"
....
objCDO1.Textbody = fso.OpenTextFile(FileToBeUsed1, ForReading).ReadAll + fso.OpenTextFile(FileToBeUsed2, ForReading).ReadAll

编辑(HTMLBody)

Dim hb
    hb = fso.OpenTextFile("c:\TheFileWithColumnsInIt.txt",ForReadin).ReadAll
    hb = "<html><body><code>" + hb + "</code></body></html>"

objCD01.HTMLBody = hb

在预知问题时(某些版本的 CDO 已记录问题),请阅读此内容

于 2013-10-17T12:00:05.687 回答