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

通过VBScript发送电子邮件:来自外部文件的HTML正文,html,email,vbscript,cdo.message,Html,Email,Vbscript,Cdo.message,我正在尝试使用HTML格式的VBScript发送电子邮件,其正文存储在外部文件中 当然,脚本使用纯文本(以及Textbody属性)可以完美地工作 我的猜测是,我使用了错误的方法打开和读取文件-它可能需要解析,而不是简单地读取 这是我的VBScript(好吧,不是我的,只是现在不记得源代码): 我的HTML文件是一封来自Outlook的完全HTML格式的电子邮件,它在Edge中显示得非常漂亮。但我也尝试过将HTML文件剥离到最精简的核心,但它也不起作用: <html> <body

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

当然,脚本使用纯文本(以及Textbody属性)可以完美地工作

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

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

我的HTML文件是一封来自Outlook的完全HTML格式的电子邮件,它在Edge中显示得非常漂亮。但我也尝试过将HTML文件剥离到最精简的核心,但它也不起作用:

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

你好

有什么建议和/或代码片段需要研究吗? 提前谢谢


另外,我想我可能需要使用类似的方法来嵌入图像:?

在代码中替换以下行:

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
我测试了一下,效果很好

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