0

我有一个文本文件:

输出.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 表格式):我正在尝试使用以下代码。邮件已发送,但邮件正文一无所获。下面是什么错误?

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

使用以下代码,我收到如下电子邮件..

Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim fso, f , objCDO1 ,BodyText
Set fso = CreateObject("Scripting.FileSystemObject")
Set objCDO1 = CreateObject("CDO.Message")
BodyText = fso.OpenTextFile("c:\Output.txt",ForReading).ReadAll
BodyText = "<html><body>" + BodyText  + "</body></html>"
objCDO1.HTMLBody = BodyText
objCDO1.TO ="sunny@abc.com"
objCDO1.From = "dontreply@bt.com (HFM)"
objCDO1.Subject = "StatS"
objCDO1.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objCDO1.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtpb.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

电子邮件

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 格式接收电子邮件?

编辑2

当我在EDIT1中使用下面的 ..它的工作。

BodyText = fso.OpenTextFile("c:\Output.txt",ForReading).ReadAll
objCDO1.HTMLBody = "<html><body><pre>" & BodyText & "</pre></body></html>"

但是..当我在EDIT1中使用下面的 ..它不起作用。

BodyText = fso.OpenTextFile("c:\Output.txt",ForReading).ReadAll
objCDO1.HTMLBody = "<html><body><font size="12"><pre>" & BodyText & "</pre></font></body></html>"
4

1 回答 1

1

HTML 不像您期望的那样工作。一方面,解析器将所有连续的空白折叠成一个空格,所以像

OPERATING SYSTEM       SERVER1    SERVER2
Windows                  1.36       4.42
Linux                    2.78       5.76

变成

OPERATING SYSTEM SERVER1 SERVER2 Windows 1.36 4.42 Linux 2.78 5.76

显示时。

如果要保留空格/换行符,请更改

BodyText = fso.OpenTextFile("c:\Output.txt",ForReading).ReadAll
BodyText = "<html><body>" + BodyText  + "</body></html>"
objCDO1.HTMLBody = BodyText

进入

BodyText = fso.OpenTextFile("c:\Output.txt",ForReading).ReadAll
objCDO1.HTMLBody = "<html><body><pre>" & BodyText & "</pre></body></html>"

或完全删除 HTML 并将消息作为纯文本发送。

于 2013-10-24T21:12:38.160 回答