【问题标题】:Email a text file's content in html format以 html 格式通过电子邮件发送文本文件的内容
【发布时间】:2013-10-29 20:09:11
【问题描述】:

我有一个文本文件:

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>"

【问题讨论】:

  • myTextBodymyHTMLBody 的值是多少?你的代码中有On Error Resume Next 吗?因为.AddAttachment Replace( "c:\output.txt" ( i ), "" ),"","" 不可能工作。在架构 URL 中还有不属于 sendusing 字段的空格。
  • 嗨 Ansgar..我改变了方式..!请参阅 EDIT1...:)
  • size="12" 更改为size='12'size=""12""。 VBScript 字符串中的双引号必须被转义(通过将它们加倍)。但是,在 HTML 代码中,用单引号替换它们会更简单。
  • @Ansgar..tried ..但我想知道没有 html 字体类型,字体等标签在这里工作..在每种情况下,邮件都是默认的 courier 新字体大小 10 format.should I go for Powershell..?.:)
  • 现在我只想在 中制作它。

标签: html vbscript html-table html-email


【解决方案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 并将消息以纯文本形式发送。

【讨论】:

  • 是的..现在我收到的电子邮件格式为 output.txt..thanks Ansgar..!!
  • @Sunny 我建议的关键部分是&lt;pre&gt;&lt;/pre&gt; 标签。
  • @Ansgar..ohh..抱歉打错了..请参阅 EDIT2
猜你喜欢
  • 1970-01-01
  • 2012-01-28
  • 2017-06-28
  • 1970-01-01
  • 1970-01-01
  • 2021-08-29
  • 2010-11-26
  • 2014-06-22
相关资源
最近更新 更多