htmlemailvbscriptcdo.message

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 = "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

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:\EMail\image008.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 ?


Solution

  • 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.