Thisforion
Thisforion

Reputation: 11

Emailing HTML file contents with VBS script

How can I get this script to load the contents of a HTML file and send it as the email body.

I keep getting an error that says

Line 8

Invalid procedure call or argument

Code: 800A0005

I have tried that and it works thanks.

But when it reads the htm file the script breaks because there are more than one “ in the file.

I am getting this error

Line: 13 Object doesn't support this property or method: 'objEmail.CreateMHTMLBody' code: 800A01B6

What can I do to fix it.

Dim fso

Set objEmail = CreateObject("CDO.Message")
objEmail.From = "[email protected]"
objEmail.Subject = "Test Email"


Const ForReading=1
Set fso = CreateObject("Scripting.FileSystemObject")
Set dict = CreateObject("Scripting.Dictionary")
BodyText = fso.OpenTextFile("C:\Users\user\Desktop\Email.htm",ForReading).ReadAll
objEmail.CreateMHTMLBody = BodyText

objEmail.AddAttachment "C:\Users\user\Desktop\test.txt"

Set fso = CreateObject("Scripting.FileSystemObject")
Set dict = CreateObject("Scripting.Dictionary")
Set file = fso.OpenTextFile ("C:\Users\user\Desktop\address.txt", 1)
row = 0
Do Until file.AtEndOfStream
line = file.Readline
dict.Add row, line
row = row + 1
objEmail.To = line

objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = _
    "127.0.0.1"
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objEmail.Configuration.Fields.Update
objEmail.Send
Loop

Upvotes: 1

Views: 3160

Answers (1)

JosefZ
JosefZ

Reputation: 30113

  • Set statement assigns an object reference to a variable or property;
  • ReadAll method reads an entire TextStream file and returns the resulting string.

Hence, next code snippet should work:

Const ForReading=1
Set fso = CreateObject("Scripting.FileSystemObject")
Set dict = CreateObject("Scripting.Dictionary")
BodyText = fso.OpenTextFile("C:\Users\user\Desktop\Email.htm",ForReading).ReadAll
' superabundant Set fso = CreateObject("Scripting.FileSystemObject")
' superabundant Set dict = CreateObject("Scripting.Dictionary")
Set file = fso.OpenTextFile ("C:\Users\user\Desktop\address.txt", 1)
' …
'
objEmail.Subject = "Test Email"
objEmail.HtmlBody = BodyText
'…

Please read Paul R. Sadowski's article VBScript To Send Email Using CDO. There is a hint how to send a webpage from a file on your machine using CreateMHTMLBody method instead of setting HTMLBody property.

Upvotes: 1

Related Questions