Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
304 views
in Technique[技术] by (71.8m points)

email - E-Mail via VBScript: HTML Bodytext from external file

I am trying to send an e-mail using VBScript in HTML format whose text body is stored in an external file.

Naturally the script works perfectly using plain text (and the Textbody property).

My guess is that I use the wrong method to open and read the file - it probably needs to be parsed and not simply read.

Here's my VBScript (well, not really mine, just don't recall the source now):

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

'************************************
'** Seting basic email information **
'************************************
Const EmailFrom = "[email protected]"
Const EmailTo = "[email protected]"
Const EmailCC = "[email protected]"
Const EmailSubject = "subject"

'***************************************
'** Setting Mail Server Configuration **
'***************************************
Const MailSendUsing = "2"
Const MailSendServer = "smtp.office365.com"
Const MailSendPort = "25"
Const MailSendUsername = "[email protected]"
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:EMailEMailTextBody.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

My HTML file is a fully html-formatted E-Mail from Outlook which displays beautifully in Edge. But I also tried an HTML file stripped to its most reduced core, and it did not work either:

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

Any suggestions and/or code snippets to study? Thanks in advance!

P.S. I guess I might need to use something like this to embed the images: https://gist.github.com/TaoK/3525090 ?

question from:https://stackoverflow.com/questions/65951738/e-mail-via-vbscript-html-bodytext-from-external-file

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Replace in the code the line:

objEmail.Htmlbody = EmailBody

with this line, that points directly to the html file:

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

The html file can include images.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...