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

问题描述

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

当然,脚本可以完美地使用纯文本(和 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>

有任何建议和/或代码片段要研究吗? 提前致谢!

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

解决方法

替换代码中的行:

Exit 247

这一行,直接指向 html 文件:

objEmail.Htmlbody = EmailBody

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

我对其进行了测试,效果很好。