Sending CDO Email via VBS with embedded attachments

Recently it was asked in another NG post:



I have an asp (classic) page from which I send mails to some users. I
select those users from a recordset and depending upon certain
condition, the messsages are sent.
Well, if I load this asp file, all the stuff works fine: the recordset,
the mails sent to the correct users (using CDOSYS in Win2003), so now
they can receive and read the messages without a single problem.

Now I need something like a demon implemented in a .vbs script. With a
bat file running every hour, i need the same functionalities: connect
and build the recordset, and sending the mails to the users in an
automated way, every hour.
But I just don't know if it's possible to do such a task. somebody has
an example of how could you send a mail from a vbs file that works
within the task manager? I don't ask for the task manager obviously,
but for the way sending mails are implemented in an automated .vbs.
I suppose it's not enough to paste part the same code (for connection
and recordset and mails) from the asp file into the vbs.
Hope my explanation is clear enough. Thanks in advance.




In response I offered up my last post : Recurring AT scheduling using VBS and batch files.
and then I proceeded to put together the code to generate an email using CDO from VBS.



I know, this has been done a thousand times, but i could not find an example
that properly implemented inline embedded images so I spent the time to write
one... enjoy...


here was the text of my reply:



...And this will permit you to send an HTML EMail with embedded images
using just VBScript and CDO accessing an outside server and providing
the appropriate credentials...

re:embedded image [ "file://C:\sounds\junk.gif" ]

You must follow that format or the embedding fails
and ... and you'll find "junk.gif" inside of the HTML body of the email too.

hth,

D.

email.vbs


' VB Script Document
option explicit
Dim
HTML
Dim contentId

Dim MyMail
Set MyMail = CreateObject("CDO.Message")
with MyMail

HTML
= "<!DOCTYPE HTML PUBLIC ""-//IETF//DTD HTML//EN"">" & chr(13) & chr(10)
HTML = HTML & "<html>"
HTML = HTML & "<head>"
HTML = HTML & "<meta http-equiv=""Content-Type"" content=""text/html; charset=iso-8859-1"">"
HTML = HTML & "<title>Automated Emails!</title>"
HTML = HTML & "</head>"
HTML = HTML & "<body bgcolor=""#FFFFFF"">"
HTML = HTML & "<p>"
HTML = HTML & "<img src=""cid:junk.gif""><br>"
HTML = HTML & "<a href=""http://www.google.com"">"
HTML = HTML & "Do a Google Search!</a>"
HTML = HTML & "</p>"
HTML = HTML & "<b>More HTML Here!</b>"
HTML = HTML & "</body>"
HTML = HTML & "</html>"


.From = "From@comcast.net"
.To = "ToEmail@hotmail.com;someoneElse@somewhere.com"
.CC = "emailAddr@somewhere.com"
.BCC = "emailAddr@somewhere.com"
.Subject = "VBS Email via CDONTS"
.HTMLBody = HTML

Const cdoSchema = "http://schemas.microsoft.com/cdo/configuration/"
.Configuration.Fields.Item(cdoSchema & "sendusing") = 2
.Configuration.Fields.Item(cdoSchema & "smtpserver") = "smtp.ISP.net"
.Configuration.Fields.Item(cdoSchema & "smtpserverport") = 25
.Configuration.Fields.Item(cdoSchema & "smtpauthenticate") = 1
.Configuration.Fields.Item(cdoSchema & "sendusername") = "ISP UserName"
.Configuration.Fields.Item(cdoSchema & "sendpassword") = "ISPPassword"
.Configuration.Fields.Update

.AddRelatedBodyPart
"file://C:\sounds\junk.gif", "junk.gif", 0

.Send

End With
Set
MyMail = Nothing






Comments

Anonymous said…
I've been searching forever trying to find this solution with no luck. THANK YOU SO MUCH FOR POSTING THIS!!!

Popular posts from this blog

Lupeo

NLE 2020 NEP