growled on Wednesday, April 13, 2005 5:15:00 PM (Pacific Standard Time, UTC-08:00)
barked at code [vbscript]
Send email from a VBScript.  It's something I've done before and just recently had to do again so I figured why not post a how2 on the subject.  It's not hard to do, but initially it did take a bit for me to wrap my pea-brain around it.
 
Here's a quick example using a remote SMTP server (the setup we use):
  1. Set objMessage = CreateObject("CDO.Message")
  2. objMessage.Subject = "Testing vbscript to send email"
  3. objMessage.Sender = "sender@microsoft.com"
  4. objMessage.From = "sender@microsoft.com"
  5. objMessage.To = "recipient@microsoft.com"
  6. objMessage.BCC = "BCCrecpient@microsoft.com"
  7. objMessage.TextBody = "Put whatever text you want here!"
  8.  
  9. '==This section provides the configuration information for the remote SMTP server.
  10. '==Normally you will only change the server name or IP.
  11.         objMessage.Configuration.Fields.Item _
  12.                 ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
  13.         'Name or IP of Remote SMTP Server
  14.         objMessage.Configuration.Fields.Item _
  15.                 ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "SMTPSERVERNAME"
  16.         'Server port (typically 25)
  17.         objMessage.Configuration.Fields.Item _
  18.                 ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
  19.         objMessage.Configuration.Fields.Update
  20. '==End remote SMTP server configuration section==
  21.  
  22. objMessage.Send
You can also easily format your email using HTML by replacing line 7 with an HTMLBody property, something like this:
objMessage.HTMLBody = "<HTML><P><B>Hello World!</B></P><P>Here I am.</P></HTML>"
Of course you can also get pretty creative with the HTMLBody property by putting several lines of HTML into a string (creating a page much more intricate than "Hello World").  For my recent script I retrieved results from a SQL db and formatted them into a table using a string and a Do While loop.  Something like this (where srchObj contains the results of the SQL query):
  1. dim strHtmlBody
  2. strHtmlBody = Nothing
  3. strHtmlBody = strHtmlBody & ("<HTML><P>The following table represents something I want to send in email.</P>")
  4. strHtmlBody = strHtmlBody & ("<TABLE BORDER=1><TR><TD><B>column1Title</TD><TD><B>column2Title</TD><TD><B>colum3Title</TD></TR>")
  5. Do While not srchObj.EOF
  6.         strHtmlBody = strHtmlBody & ("<TR><TD>" & srchObj(0) & "</TD><TD>" & srchObj(1) & "</TD><TD>" & srchObj(2) & "</TD><TR>")
  7.         srchObj.MoveNext
  8.         loop
  9. strHtmlBody = strHtmlBody & ("</TABLE><BR><BR>")
  10. strHtmlBody = strHtmlBody & ("<P>Thank you,</P></HTML>")
References:
  • link - MSDN listing of CDO configuration field settings.
  • link - MSDN listing of IMessage Interfaces (email properties like HTMLBody, ReplyTo, Subject, etc.).
  • link - Paul Sadowski has a great page (titled VBScript To Send Email Using CDO) with extensive code examples for sending email through vbs (text or html format, with attachments, using a remote SMTP server, with return receipt, etc.). 
As always, I hope this helps someone out there...
Comments are closed.