以html格式通过电子邮件发送文本文件的内容

时间:2013-10-24 02:06:17

标签: html vbscript html-table html-email

我有一个文本文件:

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

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

EDIT1

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

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格式?

EDIT2

当我在 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>"

1 个答案:

答案 0 :(得分: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并以纯文本形式发送消息。