通过 VBScript 发送电子邮件:HTML 来自外部文件的正文

E-Mail via VBScript: HTML Bodytext from external file

我正在尝试使用 VBScript 以 HTML 格式发送电子邮件,其正文存储在外部文件中。

自然地,脚本使用纯文本(和 Textbody 属性)可以完美运行。

我的猜测是我使用了错误的方法打开和读取文件 - 它可能需要被解析而不是简单地读取。

这是我的 VBScript(好吧,不是我的,只是现在不记得来源):

Dim objEmail
Set objEmail = CreateObject("CDO.Message")

'************************************
'** Seting basic email information **
'************************************
Const EmailFrom = "from@mail.com"
Const EmailTo = "to@mail.com"
Const EmailCC = "cc@mail.com"
Const EmailSubject = "subject"

'***************************************
'** Setting Mail Server Configuration **
'***************************************
Const MailSendUsing = "2"
Const MailSendServer = "smtp.office365.com"
Const MailSendPort = "25"
Const MailSendUsername = "from@mail.com"
Const MailSendPassword = "123456789"
Const MailSendAuthenticationType = "1"

'**************************************
'** Email Parameters (DO NOT CHANGE) **
'**************************************
objEmail.Sender = EmailFrom
objEmail.To = EmailTo
objEmail.CC = EmailCC
objEmail.Subject = EmailSubject
objEmail.Htmlbody = EmailBody
objEmail.AddAttachment EmailAttachments
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = MailSendUsing
ObjEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = MailSendServer
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = MailSendPort
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = MailSendAuthenticationType
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusername") = MailSendUsername
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendpassword") = MailSendPassword
objEmail.Configuration.Fields("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True

'*******************************************************
'** Setting a text file to be shown in the email Body **
'*******************************************************
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
'** File to be inserted in Body
Const FileToBeUsed = "C:\EMail\EMailTextBody.html"
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
'** Open the file for reading
Set f = fso.OpenTextFile(FileToBeUsed, ForReading, -1)
'** The ReadAll method reads the entire file into the variable BodyText
objEmail.Textbody = f.ReadAll
'** Close the file
f.Close
'** Clear variables
Set f = Nothing
Set fso = Nothing

'* cdoSendUsingPickup (1)
'* cdoSendUsingPort (2)
'* cdoSendUsingExchange (3)

'********************************
'** Parameters (DO NOT CHANGE) **
'********************************
ObjEmail.Configuration.Fields.Update
objEmail.Send

'Create the objects require for sending email using CDO
Set objMail = CreateObject("CDO.Message")
Set objConf = CreateObject("CDO.Configuration")
Set objFlds = objConf.Fields

我的 HTML 文件是来自 Outlook 的完全 html 格式的电子邮件,它在 Edge 中显示得很漂亮。但我也尝试了一个 HTML 文件,剥离到其最精简的核心,但它也没有工作:

<html>
<body>
<p>hallo!</p>
<img src="C:\EMail\image008.gif" />
</body>
</html>

有什么建议 and/or 代码片段可以研究吗? 提前致谢!

P.S。我想我可能需要使用类似这样的东西来嵌入图像:https://gist.github.com/TaoK/3525090 ?

在代码中替换行:

objEmail.Htmlbody = EmailBody

这一行直接指向 html 文件:

objEmail.CreateMHTMLBody "file://c:/path/to/htmlfile.html"

html 文件可以包含图像。

Alex-dl 提出了正确的解决方案,因此这里是要使用的完整代码。为了提供完整的答案,我还在附件部分添加了评论。

Dim objEmail
Set objEmail = CreateObject("CDO.Message")

'************************************
'** Seting basic email information **
'************************************
Const EmailFrom = "from@mail.com"
Const EmailTo = "to@mail.com"
'CC and additionally EmailBCC are optional
Const EmailCC = "CC@mail.com"
Const EmailBCC = "BCC@mail.com"
Const EmailSubject = "Subject"

'***************************************
'** Setting Mail Server Configuration **
'***************************************
Const MailSendUsing = "2"
Const MailSendServer = "smtp.office365.com"
Const MailSendPort = "25"
Const MailSendUsername = "from@mail.com"
Const MailSendPassword = "123456789"
Const MailSendAuthenticationType = "1"

'**************************************
'** Email Parameters (DO NOT CHANGE) **
'**************************************
objEmail.Sender = EmailFrom
objEmail.To = EmailTo
' Make sure to uncomment the following two line in case you send CC or BCC
'objEmail.CC = EmailCC
'objEmail.BCC = EmailBCC
objEmail.Subject = EmailSubject
objEmail.CreateMHTMLBody "C:\path\to\file.html"
' add one line per attachmenet
objEmail.AddAttachment "C:\path\to\attachment"
'objEmail.AddAttachment "C:\path\to\attachment2"
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = MailSendUsing
ObjEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = MailSendServer
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = MailSendPort
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = MailSendAuthenticationType
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusername") = MailSendUsername
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendpassword") = MailSendPassword
objEmail.Configuration.Fields("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True

'********************************
'** Parameters (DO NOT CHANGE) **
'********************************
ObjEmail.Configuration.Fields.Update
objEmail.Send

'Create the objects require for sending email using CDO
Set objMail = CreateObject("CDO.Message")
Set objConf = CreateObject("CDO.Configuration")
Set objFlds = objConf.Fields

我测试了一下,效果很好。