2013-10-17 86 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