【问题标题】:Sending two text files content as body in a mail在邮件中将两个文本文件内容作为正文发送
【发布时间】:2013-10-17 10:41:27
【问题描述】:

我有两个文本文件:

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 ="sunny@abc.com"
objCDO1.From = "dontreply@abc.com (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 表格格式):

如何以 HTML 表格的形式将 Output.txt 文件的内容附加到电子邮件正文..?

EDIT3

现在我创建了以下代码:

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

邮件已发送但邮件正文一无所获..上面的错误是什么?

【问题讨论】:

  • WScript.Echo TypeName(fso) 的输出是什么?
  • 嗨 Ansgar..当我删除 set f 功能时..上面的代码运行良好..感谢您的回复..! :)

标签: email vbscript


【解决方案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

编辑(HTMLBody)

Dim hb
    hb = fso.OpenTextFile("c:\TheFileWithColumnsInIt.txt",ForReadin).ReadAll
    hb = "<html><body><code>" + hb + "</code></body></html>"

objCD01.HTMLBody = hb

在预知问题(某些版本的 CDO 已记录问题),请阅读this

【讨论】:

  • 请参阅有问题的 EDIT1
  • 你不需要设置 f= ... 文件的打开和读取在分配给 Textbody 属性时就完成了。
  • @MC..我无法使用上述代码发送 excel 文件的内容。在收到的邮件中只有一些混乱的符号出现。为什么会这样?
  • TextBody 仅包含文本。设置TextBody后需要将excel文件附加到消息objCD01.AddAttachment "C:\WhatEver\YourFile.xls"
  • 但我认为这会将 excel 文件作为附件而不是其内容作为正文发送..如果我没记错的话..:)
猜你喜欢
  • 2015-05-10
  • 1970-01-01
  • 2015-12-19
  • 2018-05-06
  • 2020-06-08
  • 2015-03-27
  • 1970-01-01
  • 2018-04-29
  • 2015-10-02
相关资源
最近更新 更多