Click here to Skip to main content
13,794,242 members
Click here to Skip to main content
Add your own
alternative version


26 bookmarked
Posted 22 Dec 2002

SMTP Server Transport Events - Simple Scripting

, 26 Dec 2002
Rate this:
Please Sign up or sign in to vote.
An article on SMTP Server Events
<!-- Download Links --> <!-- Add the rest of your HTML here -->


This article describes SMTP Transport Events in the Microsoft SMTP server. The article shows how to write simple scripts and how to install these scripts. The article also shows how to write incoming emails to a database and how to mark incoming emails with a red follow up flag.


Recently I was asked to work out a way to get Exchange 2000 to mark some incoming emails with a red follow up flag. I started looking through MSDN and I came across SMTP Server Events. I discovered that there are 2 types of events:

  • Protocol Events
  • Transport Events
This article is going to discuss transport events.

Transport events occur when an email has been received by the SMTP server. The event that I want to discuss is the OnSubmission event, there are 2 other events: OnPreCategorize and OnPostCategorize. The OnTransportSubmission event is the same event as the CDO for Windows 2000 OnArrival event. The OnTransportSubmission event is called with the following prototype:

Sub ISMTPOnArrival_OnArrival(ByVal Msg, EventStatus)

A CDO MailMsg object is passed into the sub routine, with this we have full access to the email that the server is processing. So the full code to add a red follow up flag would be:

On Error Resume Next

Const cdoRunNextSink = 0

Sub ISMTPOnArrival_OnArrival(ByVal Msg, EventStatus)
    On Error Resume Next 
        Set Flds = Msg.Fields
        With Flds
            .Item("urn:schemas:httpmail:messageflag") = True
            .Item("urn:schemas:mailheader:x-message-flag") = True
        End With
    ' save changes to the mail
    ' continue with the next event sink
    EventStatus = cdoRunNextSink

End Sub		

On another project we wanted to automatically add bounced email to our database, the code to do this would look something like this:

On Error Resume Next

Const cdoRunNextSink = 0

Const adCmdStoredProc = &H0004
Const adVarChar = 200
Const adWChar = 130
Const adParamInput = &H0001

Sub ISMTPOnArrival_OnArrival(ByVal Msg, EventStatus)

  On Error Resume Next 

    Dim objConnection, objCommand, strConnection, intLen
    Dim strBody, strToEmail, strFromEmail, strSubject

    strToEmail = Msg.To
    strFromEmail = Msg.From
    strSubject = Msg.Subject
    strBody = Msg.TextBody

    If strBody <> "" Then
      intLen = Len(strBody) + 1
      intLen = 1
    End If

    strConnection = _
      "Provider=SQLOLEDB;SERVER=localhost;Initial Catalog=databasename;User ID=;Password=;"

    Set objConnection = CreateObject("ADODB.Connection")
    Set objCommand = CreateObject("ADODB.Command")

    Call objConnection.Open(strConnection)

    objCommand.ActiveConnection = objConnection
    objCommand.CommandType = adCmdStoredProc
    objCommand.CommandText = "spAddBouncedEmail"

    objCommand.Parameters.Append objCommand.CreateParameter("ToEmail", adVarChar, _
        adParamInput, 255, strToEmail)
    objCommand.Parameters.Append objCommand.CreateParameter("FromEmail", adVarChar, _
        adParamInput, 255, strFromEmail)
    objCommand.Parameters.Append objCommand.CreateParameter("Subject", adVarChar, _
        adParamInput, 255, strSubject)
    objCommand.Parameters.Append objCommand.CreateParameter("Body", adWChar, _
        adParamInput, intLen, strBody & " ")

    ' continue with the next event sink
    EventStatus = cdoRunNextSink

End Sub

Using the code

Microsoft were very kind to provide us with a VB script that helps us to install our scripts, it's called smtpreg.vbs
To install a script:

cscript smtpreg.vbs /add 1 onarrival BouncedEmail CDO.SS_SMTPOnArrivalSink 
    "rcpt to=*;rcpt to=*"
This adds an event called BouncedEmail onarrival with an entry point of ISMTPOnArrival_OnArrival which will be applied to all recipients that match '*' and '*'.

Now we need to tell the server which script to run:

cscript smtpreg.vbs /setprop 1 onarrival BouncedEmail Sink ScriptName "path_to_scriptfile"
This just sets the property on the BouncedEmail event, telling it where to locate the script.

Un-installation is a simple process:

cscript smtpreg.vbs /remove 1 onarrival BouncedEmail

Points of Interest

I came across an interesting problem whilst implementing the red follow-up flag. The client wanted incoming emails to be marked with the follow up as well as internal emails going to a certain address. At first this wasn't working for internal addresses, this is because Outlook uses MAPI to communicate with exchange rather than SMTP. Microsoft issued this KB Article explaining the problem. The work around is pretty messy, it involves adding a new virtual SMTP server and bouncing emails off it, this makes the event fire because it goes from outlook to exchange as MAPI then from exchange to the virtual SMTP server back to exchange again.

I hope this simple article can provoke some thought. I might mention that you can write your own COM add-ins that perform the same functions, of course these would give better performance. I wouldn't like to run these VB scripts on a server that has a very high demand, you might notice some performance problems. The projects I've used these techniques on have suffered no such problems however.

Some interesting links that relate to SMTP Transport Events:


  • 20/12/2002 - Article Created


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
Australia Australia
I've been programming for a few years now. I blog regularly at httpcode.

You may also be interested in...

Comments and Discussions

QuestionHOw to Grab the mails before Reaching the outlook Pin
PraxUnited2-Jul-07 1:55
memberPraxUnited2-Jul-07 1:55 
QuestionHelp requred To filter Email Content Pin
Rampun4-Jun-07 1:16
memberRampun4-Jun-07 1:16 
Generalevent doesn't fire question Pin
tamertamertamer1-Nov-06 2:11
membertamertamertamer1-Nov-06 2:11 
GeneralRe: event doesn't fire question Pin
DannSmith8-Nov-06 7:12
memberDannSmith8-Nov-06 7:12 
Questionwhat's the best way to see WHY the script doesn't run ? Pin
yogiberr26-Aug-05 1:45
memberyogiberr26-Aug-05 1:45 
GeneralSpecific Mailbox Pin
Rom200018-Aug-05 1:42
memberRom200018-Aug-05 1:42 
GeneralFollowUp Pin
Bijan Kianifard10-Jul-04 17:37
memberBijan Kianifard10-Jul-04 17:37 
GeneralFilter outgoing emails Pin
Anonymous13-Jun-04 23:26
memberAnonymous13-Jun-04 23:26 
QuestionCould fire ISMTPOnArrival_OnArrival event without Exchange server? Pin
ericchen18-Feb-04 17:16
memberericchen18-Feb-04 17:16 
AnswerRe: Could fire ISMTPOnArrival_OnArrival event without Exchange server? Pin
metweek30-Jul-06 20:59
membermetweek30-Jul-06 20:59 
GeneralAdding Envelope-From and To Pin
bernieboy222-Nov-03 13:31
memberbernieboy222-Nov-03 13:31 
QuestionMessage ID? Pin
Anonymous29-Aug-03 14:09
memberAnonymous29-Aug-03 14:09 
GeneralQuestion Pin
nonahelmi30-Jul-03 17:16
membernonahelmi30-Jul-03 17:16 
Hello Prof,

I read you project & injoy it.
I'm Searching V.C project about below topic,If you have one it Please as soon as possible send to me.
1.Design Virus
2.Intrusion Detection System
3.desing User password Detector
4.Simple Firewall
5.Dictionary attack on password files
6.Design password detection in SSL


GeneralTrouble with event sinks Pin
jdeweerdt4-Jun-03 4:51
memberjdeweerdt4-Jun-03 4:51 
GeneralRe: Trouble with event sinks Pin
Dan_P4-Jun-03 17:02
memberDan_P4-Jun-03 17:02 
GeneralRe: Trouble with event sinks Pin
Anonymous20-Jan-04 3:21
memberAnonymous20-Jan-04 3:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web04 | 2.8.181207.3 | Last Updated 27 Dec 2002
Article Copyright 2002 by Dan_P
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid