Windows Scripting
  Home arrow Windows Scripting arrow Page 4 - Sending Emails Using CDO in WSH
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
WINDOWS SCRIPTING

Sending Emails Using CDO in WSH
By: Nilpo/Developer Shed Staff Writer
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 6
    2007-04-03

    Table of Contents:
  • Sending Emails Using CDO in WSH
  • Preparing your message
  • Configuring the SMTP server
  • Sending HTML messages

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Sending Emails Using CDO in WSH - Sending HTML messages


    (Page 4 of 4 )

    Okay, so sending text emails isn’t enough for you?  This is the age of modern email after all and in most cases you’ll want to send email with clickable links and embedded images.  In this case, you’ll want to send HTML formatted emails.

    CDOSYS says “no problemo!”  It can handle those as well.  You’ll have two choices.  You can either send an HTML formatted text string or you can tell CDOSYS to send an HTML document as your email body.  The document can be either local or remote.

    We’ll start with the first and send an HTML formatted text string.  You’re going to use the same email script we’ve just created.  This time you’re going to leave out the Textbody property and replace it with the appropriate HTML property or method.

    object.HTMLbody

    object.CreateMHTMLbody path [, flags [, username, password]]

    The HTMLbody property returns or sets the HTML body part of your message.  The CreateMHTMLbody method will create an email message from the HTML document you specify.  The path may be remote or local but must be a full local path or a fully qualified URI.  The flags attribute accepts a constant value that tells the CDOSYS object what parts, if any, should be omitted from the message.  Finally, you may supply a username and password if the HTML document you specify is password protected.

    Fully qualified URIs must begin with a valid protocol.  Here are common examples:

    • http://www.mysite.com/index.html
    • file:///C:/pages/index.htm

    So if we wanted to send our local HTML page, we would change our script by replacing the first section of code with the section below.  All I’ve done is removed the line setting the Textbody property and replaced it with a line that sets the HTMLbody property instead.  Of course, I’ve formatted the text in HTML as well.

    Set objMessage = CreateObject("CDO.Message")

    ' Set Email Headers

    objMessage.From = "sender@mymail.com"

    objMessage.To = "recipient@mail.com"

    objMessage.CC = "copyto@mail.com"

    objMessage.BCC = "blindcopy@mail.com"

    objMessage.ReplyTo = "replyto@mail.com"

    objMessage.Subject = "Test email with CDOSYS!"

    ' Construct Email Body

    objMessage.HTMLbody = "<h1>This is a test email using CDOSYS.</h1>"

    To send a local or remote HTML document, we would replace the same line with a call to the CreateMHTMLbody method instead.  Here’s an example of what that code might look like.

    Set objMessage = CreateObject("CDO.Message")

    ' Set Email Headers

    objMessage.From = "sender@mymail.com"

    objMessage.To = "recipient@mail.com"

    objMessage.CC = "copyto@mail.com"

    objMessage.BCC = "blindcopy@mail.com"

    objMessage.ReplyTo = "replyto@mail.com"

    objMessage.Subject = "Test email with CDOSYS!"

    ' Construct Email Body

    objMessage.CreateMHTMLbody "C:pagesindex.htm"

    The rest of your script would remain exactly the same.  However, sending HTML messages does raise one problem.  Some people are still using email clients that do not support HTML messages.

    In those cases your may want to send what’s known as a multi-part message that includes both an HTML and a plain text portion.  In this case, the HTML is displayed by default, and the plain text is displayed whenever HTML is not supported. 

    CDOSYS makes sending multi-format messages very easy.  Just add the following line and let the AutoGenerateTextBody property do the rest.

    objMessage.AutoGenerateTextBody = True

    The AutoGenerateTextBody property tells CDOSYS whether or not to auto-generate a multi-format message by creating a text portion from the HTML part that you have provided.  Its default is False.

    You now have everything you need to send email messages in WSH.  Keep in mind that the CDO objects do provide much more functionality than I have been able to include in this article.  You can request Deliver Service Notifications and Read Receipts, check local email boxes, access local Address Books, check and post to newsgroups, and more.  Unfortunately, I don’t have enough room to cover it all, so I’m forced to wrap this up.  Until next time, keep coding!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · In this article I'm going to show you how to add email capabilities to your scripts...
       · This is very good, but the object name on page 3 should be objMessage not...
       · You are exactly right. Thanks for the correction.
     

    WINDOWS SCRIPTING ARTICLES

    - Introducing Two-Way Data Binding using Silve...
    - Silverlight 2.0 Application Development with...
    - Burning Multisession CDs with IMAPI2 in WSH
    - Creating a Silverlight 2.0 Application that ...
    - Burning CDs with the IMAPI2 Control
    - Burning CDs in Windows XP with WSH
    - Advanced Word Object Scripting
    - Reading and Printing Word Documents in WSH
    - Scripting Microsoft Word
    - Using WSH to Catalog MP3 Files
    - Reading MP3 ID3 Tags in WSH
    - A Brief Look at Menus in WPF
    - More Examples of Simplified Image Processing...
    - Completing a WPF To-Do List Application
    - Simplified Image Processing in GDI+





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT