Windows Scripting
  Home arrow Windows Scripting arrow Page 2 - 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  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
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 - Preparing your message


    (Page 2 of 4 )

    We’ll begin by setting our basic email fields: To, From, CC, BCC, etc.  These are the fields used to construct the email’s header.

    Properties

    object.To

    object.From

    object.CC

    object.BCC

    object.ReplyTo

    object.Subject

    object.Textbody

    object.HTMLBody

    object.Configuration.Fields.Item(schema)

     

    Methods

    object.Configuration.Fields.Update

    object.CreateMHTMLBody htmlbody

    object.AddAttachment file

    object.Send

    All of the properties listed above accept text strings and are used to set various email fields.  I’ll go into details about them as we use them.  These are much easier to learn when you see them in action.

    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.Textbody = "This is a test email using CDOSYS."

    Here you can see I’ve set the standard email fields.  Only the From, To, and Textbody fields are required.  The ReplyTo field is the address that replies should be sent to if you don’t want them going to the From email address by default.

    All of the email address fields may use any standard email address format:

    • "Full Name" <email@domain.com>
    • "Full Name" email@domain.com
    • Full Name <email@domain.com>
    • email@domain.com

    You may add multiple recipients to any of the address fields by separating multiple addresses with commas.  If you use any of the formats involving quotation marks, you will need to escape them in your script.

    The Textbody property accepts a single text string as a value.  This can have any number of carriage returns as needed.  The text string can be read from an existing text file, read from any TextStreamObject (such as a program’s output) or coded manually.

    object.AddAttachment localpath | remoteurl[, username, password]

    Want to add some attachments?  That’s no problem either.

    objMessage.AddAttachment "C:attachment.txt"

    AddAttachment can accept either a local file path or a valid URL.  If the URL is password protected, be sure to add the username and password as well.  You may call AddAttachments as many times as needed to add multiple attachments to your message.

    More Windows Scripting Articles
    More By Nilpo/Developer Shed Staff Writer


       · 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

    - More Windows Scripting Workarounds from Nilpo
    - Overloading Methods and More in VBScript
    - Improving MFC for Windows Vista
    - Regular Expressions in VBScript
    - Working with Dates in WMI
    - Completing Calendars with VBScript Date Func...
    - Building Calendars with VBScript Date Functi...
    - Working With Dates and Times in VBScript
    - Designing WCF DataContract Classes Using the...
    - Understanding Dates and Times in VBScript
    - Working With Arrays in VBScript
    - Compressed Folders in WSH
    - Using .NET Interops in VBScript
    - Nilpo`s Scripting Secrets, Vol I
    - Database operations using Silverlight 2.0 WC...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek