2013-10-17 60 views
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 ="[email protected]" 
objCDO1.From = "[email protected] (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表格格式):

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

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 ="[email protected]" 
objEmail.From = "[email protected] (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 

邮件发送,但在邮件正文越来越nothing..what是上述错误:

EDIT3

现在我有下面的代码产生的?

+0

什么'WScript.Echo TypeName(fso)'的输出? –

+0

嗨Ansgar..when我删除**设置f **功能..上述代码工作完美..感谢您的回应..! :) – Sunny

回答

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 

EDIT(HTMLBody)

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

objCD01.HTMLBody = hb 

而且在出现问题预知(CDO的某些版本中有记载的问题),请阅读this

+0

请参阅** EDIT1 **问题。 – Sunny

+1

您不需要设置f = ...文件的打开和读取在对Textbody属性赋值时完成。 –

+0

伟大的MC ..现在完美工作..!..非常感谢.. :) – Sunny