Email a text file's content in html format

醉酒当歌 提交于 2019-12-11 20:11:28

问题


I have a text file as:

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

I want to send content of Output.txt in a email as a body sothat its format (alignment) doesn't get changed (like an HTMIL table format): i am trying with below code. Mail sent but in mail body getting nothing..what is error below ?

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

With below code, i am getting email as follows ..

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

email

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 

why not getting email as output.txt format ?

EDIT2

When I am using below in EDIT1..its working.

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

But..when I am using below in EDIT1..its not working.

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

回答1:


HTML doesn't work the way you seem to expect. For one thing, the parser collapses all consecutive whitespace to a single space, so something like

OPERATING SYSTEM       SERVER1    SERVER2
Windows                  1.36       4.42
Linux                    2.78       5.76

becomes

OPERATING SYSTEM SERVER1 SERVER2 Windows 1.36 4.42 Linux 2.78 5.76

when displayed.

If you want spaces/newlines preserved, change

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

into

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

or drop the HTML altogether and send the message as plain text.



来源:https://stackoverflow.com/questions/19555640/email-a-text-files-content-in-html-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!