在邮件中将两个文本文件内容作为正文发送

时间:2013-10-17 10:41:27

标签: email vbscript

我有两个文本文件:

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

请帮帮我。

EDIT1

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

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行的抛出运行时错误:

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

EDIT2

我有一个文本文件:

output.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文件内容附加到电子邮件正文中。?

EDIT3

现在我创建了以下代码:

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

邮件已发送但在邮件正文中一无所获......以上是什么错误?

1 个答案:

答案 0 :(得分: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记录了问题),请阅读this