asp tutorials, asp.net tutorials, sample code, and Microsoft news from 15Seconds
Data Access  |   Troubleshooting  |   Security  |   Performance  |   ADSI  |   Upload  |   Email  |   Control Building  |   Component Building  |   Forms  |   XML  |   Web Services  |   ASP.NET  |   .NET Features  |   .NET 2.0  |   App Development  |   App Architecture  |   IIS  |   Wireless
 
Pioneering Active Server
 Power Search








Active News
15 Seconds Weekly Newsletter
• Complete Coverage
• Site Updates
• Upcoming Features

More Free Newsletters
Reference
News
Articles
Archive
Writers
Code Samples
Components
Tools
FAQ
Feedback
Books
Links
DL Archives
Community
Messageboard
List Servers
Mailing List
WebHosts
Consultants
Tech Jobs
15 Seconds
Home
Site Map
Press
Legal
Privacy Policy
internet.commerce














internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

HardwareCentral
Compare products, prices, and stores at Hardware Central!

Implementing an E-mail Content Filter Using CDO
By George Walker
Rating: 4.2 out of 5
Rate this article


  • email this article to a colleague
  • suggest an article

    Introduction

    Junk e-mail is a common problem in the modern Internet. Mailboxes become flooded with e-mail not directly intended for users, and users are subjected to rude or disgusting messages. Many commercial solutions for this problem exist, however it is easy to create a custom solution to the problem using Microsoft Collaboration Data Objects (CDO) for Windows 2000, an application program interface (API) that is part of all versions of Windows 2000 Server.

    SMTP Service

    The Microsoft Simple Mail Transport Service (SMTP) is a component of Windows 2000 Server's Internet Information Services (IIS)5.0. The SMTP service is capable of generating two types of events, Protocol events and transport events. Protocol events are events relating to the transmission of raw data and transport events relate to outcomes of a protocol event, such as the delivery of e-mail or a newsgroup post. CDO has an easy-to-use interface to a subset of the SMTP service events. The event used for the spam filter is the OnArrival event.

    OnArrival Event

    The OnArrival event occurs when a message has been successfully received by the SMTP service and before the message has been written to the drop directory or processed by the mail server (such as Microsoft Exchange Server). It is possible for the message to be aborted, which is why this event is used for the spam filter. The header for the OnArrival event (when implemented in VBScript) is as follows:

    
    Sub ISMTPOnArrival_OnArrival(ByVal oMsg, intEventStatus)
    
    
    oMsg is a CDO Message object that is passed by value. It is possible to make changes to this object persistent by using methods of the Message class. intEventStatus is an integer result used by CDO to determine if any further event sinks need to be triggered after the current one is complete.

    Spam Detection

    Once the OnArrival event fires, the message object is available for scrutiny. In the example solution, the message subject and body are searched for a series of stopwords; if any are found, the message is rejected. An alternate implementation could compare the message FROM field against a list of domains known to be spam.

    Spam Rejection

    In the example solution spam rejection is a three-step process:

    1. Send a "rejection message" to the sender to let them know that the message was rejected, why it was rejected, and contact information in case the message was important.
    2. Log the rejection to a text file, recording the reason, relevant message header fields, and the body of the message.
    3. Abort the delivery of the message.
    CDO is used to send the rejection message, as shown in the following code:
    
    Dim oRejectionMessage
    Set oRejectionMessage = CreateObject("CDO.Message")
    oRejectionMessage.To = oMsg.From
    oRejectionMessage.From = strPostmasterEmail
    oRejectionMessage.Subject = "Mail rejected"
    oRejectionMessage.TextBody = "Your message with the subject & _ 
    """ & oMsg.Subject & """ addressed to " & oMsg.To & _ 
      " was rejected by a content filter due to the presence " & _ 
      "of the phrase or word `" & strStopWord & "`."
    oRejectionMessage.Send
    Set oRejectionMessage = Nothing  'release the object reference
    
    
    A FileStream object (Scripting.FileSystemObject.TextStream) is used to append data to the log file:
    
          ' log the message
    
          Dim oFS
          Set oFS = CreateObject("Scripting.FileSystemObject")
          Dim oLog
          Set oLog = oFS.OpenTextFile("c:\spam.log", 8, True )
          oLog.Write "-----------------------------------------------" & vbCrLf
          oLog.Write "strStopWord `" & strStopWord & "` detected." & vbCrLf
          oLog.Write "From: " & oMsg.From & vbCrLf
          oLog.Write "To: " & oMsg.To & vbCrLf
          oLog.Write "Subject: " & oMsg.Subject & vbCrLf & "Date: & _ 
    	" & now() & vbCrLf & vbCrLf
          oLog.Write oMsg.TextBody & vbCrLf & vbCrLf
          oLog.Close
    
    
    Messages are aborted by altering the MessageStatus envelope field. This field is retrieved from the EnvelopeFields collection of the message. The message is then saved back to the data store using the CDO Message.DataSource.Save() command.
    
          Dim oFlds
    Set oFlds = oMsg.EnvelopeFields
    oFlds("http://schemas.microsoft.com/cdo/smtpenvelope/messagestatus") = 3 ' bad mail, abort delivery
          oFlds.Update ' must call update to commit changes to fields
          oMsg.DataSource.Save()' save the message to the message store
    
    
    Finally, the event's EventStatus parameter is set to 1 to indicate that no further event sinks should be triggered. (It is possible to register multiple event sinks for a given event.)

    Installation

    To register the event, the smtpreg.vbs script is used. This script is available from Microsoft by downloading the Platform SDK section on CDO for Windows 2000. (See http://www.microsoft.com/msdownload/platformsdk/sdkupdate/)

    This is a two-step process. First, an event sink is created using the following command:

    
    cscript smtpreg.vbs /add     1 onarrival SMTPScriptingHost CDO.SS_SMTPOnArrivalSink "mail from=*"
    
    
    This command adds an OnArrival event sink to the SMTP service. The sink is registered in location 1, and so will be processed first (other event sinks may be registered by specifying a different location). While the SMTPScriptingHost indicates that the event sink will be a VBS file, event sinks may also be created using C++ or other compiled languages. The keyword CDO.SS_SMTPOnArrivalSink specifies that class will handle the sink, and "mail from=*" specifies that mail from anyone will be sent to the sink.

    The second command used to register the sink is:

    
    cscript smtpreg.vbs /setprop 1 onarrival SMTPScriptingHost Sink ScriptName "c:\path\to\spamfilter.vbs"
    
    
    This command sets the ScriptName property of the previously declared event sink to our VBS file. This file will be interpreted when the event fires.

    The complete source code for the spam filter is available for download here.

    Testing

    To test the event sink, install it on a server equipped with Microsoft Exchange 2000 or another mail system that uses CDO, and send a message with one of the specified stopwords in it. This should result in the generation of a rejection message, a log notation, and the abortion of the message's delivery. A variation of this script has been used by the author to filter content on a medium-sized Web server (providing virtual Web and e-mail hosting for 50 domains and a few hundred users); no noticeable performance drop occurred after the script was installed, and junk e-mail volume decreased dramatically.

    Conclusion

    CDO for Windows 2000 is a powerful API that can be used to create a content filter for e-mail. The filter may be implemented using a compiled language such as C++ (by creating a registered ATL COM object) or by using Visual Basic Scripting Edition to write an event handler.

    About the Author

    George Walker is a computer system technologist in Victoria, British Columbia, Canada. He is the senior systems analyst at Sage Internet Solutions, Ltd. and a Director of E-Scape Systems, Inc. He may be reached by e-mail at george@escapesystems.com.

    References

    Platform SDK: CDO for Windows 2000 -- Supported Transport Events with CDO
    http://msdn.microsoft.com/library/en-us/cdosys/html/_cdosys_supported_transport_events_with_cdo.asp

    Microsoft Windows 2000 SMTP Service Events by Microsoft's David Lemson.
    This April 2000 document gives system administrators, project managers, and developers an overview of the Microsoft Windows 2000 SMTP service events.
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnsmtps/html/transportevents.asp

    Platform SDK: CDO for Windows 2000 -- Registering Script Sink Bindings
    http://msdn.microsoft.com/library/en-us/cdosys/html/_cdosys_registering_script_sink_bindings.asp

  • Rate This Article
    Not HelpfulMost Helpful
    1 2 3 4 5
    Supporting Products/Tools
    AspEmail
    Free SMTP component that supports multiple file attachments, unlimited recipients, CC's, BCC's and REPLY-TO's. Sends messages as plain text or in the HTML format. Premium features include message queuing and deferred processing for high mail volumes. When used with AspEncrypt, generates S/MIME-enabled secure mail.
    [Top]
    AspMail
    AspMail supports multiple file attachments (MIME and UUE), US ASCII and ISO-8859-1 character sets, 8bit subject lines, custom message content headers, custom message headers, MS Exchange priority headers, PGP and more.
    [Top]
    DevMailer 1.0
    DevMailer adds SMTP email sending abilities to ASP or Perl programs. Features include: attachments, failsafe queueing, redundant servers, standard message file support, and advanced activity logging. Also verify email addresses and send multiple messages on a single connection.
    [Top]
    JangoMail
    JangoMail, located at JangoMail.com, is a web-based service that sends mass e-mails by connecting to data from your SQL Server or ODBC compliant database. Unlike traditional ASP e-mail components, the JangoMail service can also handle unsubscribes and bounces automatically and synchronize these with your original web database. The only setup that is required is the placement of one ASP file on your web server. Other features include message open tracking and click tracking.
    [Top]
    JMail
    Send Email directly from you web page via your webserver. jMail will not start up any annoying email clients, just smoothly send the mail via the mailserver. Implement it with easy ASP code.
    [Top]
    Other Articles
    Sep 29, 2005 - Migrating to a Load Balanced IIS 6 Environment
    Migration to IIS 6 can present itself as a daunting challenge. Depending on your existing hosting configuration, the process can number in hours, days, or even weeks. Careful planning and research is integral to achieve a successful migration.
    [Read This Article]  [Top]
    Apr 18, 2003 - IIS 6.0: Lessons in Trustworthy Computing
    Microsoft's Trustworthy Computing initiative significantly changed the way in which Microsoft builds and designs software. In this article, Jeff Gonzalez explores some of the new options and architecture in Internet Information Services 6.0.
    [Read This Article]  [Top]
    Mar 25, 2002 - Moving IIS To A Different Server - Part 2
    Brien Posey evaluates two additional methods for migrating a Web server and it settings, backup/restore and ghosting.
    [Read This Article]  [Top]
    Feb 27, 2002 - Moving Your IIS Server to a New Server - Part 1
    Upgrading your server? Brien Posey takes a look at the process and pitfalls of migrating IIS to a completely different server.
    [Read This Article]  [Top]
    Jan 23, 2002 - Troubleshooting IIS Access Problems
    Spending countless hours developing a Web site only to discover that no one can access it is frustrating. This article guides you through the process of troubleshooting Web-site access problems.
    [Read This Article]  [Top]
    Jan 18, 2002 - Running IIS on Windows XP Home Edition?
    Members of the 15Seconds discussion list may have found a way to run IIS on Windows XP Home Edition, so developers can run ASP pages. Attempt at your own risk!
    [Read This Article]  [Top]
    Dec 27, 2001 - Working With IIS Packet Filtering
    Brien Posey discusses IP packet filtering and other ways in which to control access through IIS.
    [Read This Article]  [Top]
    Oct 30, 2001 - Protecting Your IIS Server and Web Application
    Internet viruses such as Code Red and Nimbda have brought down numerous IIS Web servers recently. Fortify and defend your system with this comprehensive strategy authored by 30-year industry veteran, Andrew Novick.
    [Read This Article]  [Top]
    Feb 10, 2000 - Creating Dynamic JavaScript with ASP and Databases
    Travis Giggy demonstrates how to put ASP tags inside of JavaScript blocks so developers can fit large amounts of data into one form on a single page. He offers an overview of things that can be done with dynamic JavaScript with ASP and data queries.
    [Read This Article]  [Top]
    Mar 25, 1998 - Collaboration Data Object and IIS 4.0
    Collaboration Data Object (CDO) is a COM library designed to send mail through SMTP or Microsoft Exchange. If you install the SMTP server that comes with Microsoft Option Pack 4, you can send mail from an Active Server page using CDO. Because CDO is comes with Microsoft Option Pack 4, CDO is free.
    [Read This Article]  [Top]
    Mailing List
    Want to receive email when the next article is published? Just Click Here to sign up.

    Support the Active Server Industry



    JupiterOnlineMedia

    internet.comearthweb.comDevx.commediabistro.comGraphics.com

    Search:

    Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

    Jupitermedia Corporate Info


    Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

    Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

    Solutions
    Whitepapers and eBooks
    Microsoft Article: HyperV-The Killer Feature in WinServer ‘08
    Avaya Article: How to Feed Data into the Avaya Event Processor
    Microsoft Article: Install What You Need with Win Server ‘08
    HP eBook: Putting the Green into IT
    Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
    Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
    Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
    Avaya Article: Setting Up a SIP A/S Development Environment
    IBM Article: How Cool Is Your Data Center?
    Microsoft Article: Managing Virtual Machines with Microsoft System Center
    HP eBook: Storage Networking , Part 1
    Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
    MORE WHITEPAPERS, EBOOKS, AND ARTICLES
    Webcasts
    Intel Video: Are Multi-core Processors Here to Stay?
    On-Demand Webcast: Five Virtualization Trends to Watch
    HP Video: Page Cost Calculator
    Intel Video: APIs for Parallel Programming
    HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
    Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
    MORE WEBCASTS, PODCASTS, AND VIDEOS
    Downloads and eKits
    Sun Download: Solaris 8 Migration Assistant
    Sybase Download: SQL Anywhere Developer Edition
    Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
    Red Gate Download: SQL Compare Pro 6
    Iron Speed Designer Application Generator
    MORE DOWNLOADS, EKITS, AND FREE TRIALS
    Tutorials and Demos
    How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
    eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
    IBM Article: Collaborating in the High-Performance Workplace
    HP Demo: StorageWorks EVA4400
    Microsoft How-to Article: Get Going with Silverlight and Windows Live
    MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES