Adding Disclaimer to outgoing SMTP messages - Using Visual Basic script or Visual Basic
By frankyu
Created 2005-09-02 23:38

  • Exchange 2000

Introduction

In this Article, I will show you the steps to use an SMTP transport event sink to add a disclaimer to outgoing SMTP e-mail messages. Many organizations choose the option to add a disclaimer at the end of every message that leaves the internal Mail server to the internet and sometimes goes to every internal message. These disclaimers come in different format, but focusing on legal standpoint, emphasizing that the individual or group who sends the message holds no accountability for maltreating the contents of the message. It also warns the potential recipients to wipe out the messages if they receive or view it by accidentally.

Disclaimer is very popular now in many organizations. Earlier in the Exchange 5.5 days, Microsoft had developed a specific DLL for enabling Disclaimer (IMSEXT.DLL) – this was open to public, though it was not supported by Microsoft. It also lacks the user friendliness as this DLL required us to add the test in Rich Text Format (RTF) to a specific value in the Registry. 

With the Microsoft Exchange 2000 release it removed the DLL functionality and introduced a new method to add disclaimers by using “SMTP event sinks”.

SMTP Event Sinks

Windows 2000/2003 SMTP services can be extended using “event sinks”. Event sinks can process a message as it passed through the system and it can modify the behavior of the SMTP service.

SMTP service event trigger when there is an activity within the SMTP service, such as the transmission or arrival of an SMTP command (In other words, the submission of a message into the SMTP service transport component). As a result of event occurrence, the SMTP service uses an event dispatcher to notify registered event sinks of event. 

Exchange 2000/2003 supports three general category service events:

  •  Protocol Events: Protocol events occur at the SMTP command verb level between the client and the server when commands are either received or transmitted over the network, such as the delivery of a message locally or externally.
  • Transport Events: Transport events occur when the SMTP service receives a message, and that message passes through the SMTP stack.
  • Store events: Store eventsare used to build procedures that reacts to events in the Exchange store.

SMTP event sinks must run on the computer where the mail is being processed (recommended in bridge-head server, as all the external mails flow through it).

Create the event sink using VBScript

To create an event sink, paste the following code in a new file and save it as EventSinkScript.vbs:

 <SCRIPT LANGUAGE="VBScript">

Sub ISMTPOnArrival_OnArrival(ByVal Msg, EventStatus)

TextDisclaimer = vbCrLf & "DISCLAIMER:" & vbCrLf & "Sample Disclaimer added in a VBScript."

HTMLDisclaimer = "<p></p><p>DISCLAIMER:<br>Sample Disclaimer added in a VBScript."

If Msg.HTMLBody "" Then

'Search for the "</body>" tag and insert our discliamer before that tag. pos = InStr(1, Msg.HTMLBody, "</body>", vbTextCompare) szPartI = Left(Msg.HTMLBody, pos - 1) szPartII = Right(Msg.HTMLBody, Len(Msg.HTMLBody) - (pos - 1)) Msg.HTMLBody = szPartI + HTMLDisclaimer + szPartII End If If Msg.TextBody "" Then Msg.TextBody = Msg.TextBody & vbCrLf & TextDisclaimer & vbCrLf

End If

'Commit the content changes to the transport ADO Stream object.

Msg.DataSource.Save ' Commit the changes into the transport Stream

pEventStatus = cdoRunNextSink

End Sub

</SCRIPT>

Register the event sink

To register the event sink, use the Smtpreg.vbs file that is installed with the Exchange software development kit (SDK).

1.         At a command prompt, locate the \Exchange SDK\SDK\Support\CDO\Scripts folder, and then type the following: cscript smtpreg.vbs /add 1 OnArrival SMTPDisclaimer SMTPEventSink.Disclaimer "mail from=*@your-domain-here.com"

If this command succeeds, you receive a success message that is generated by the script.

2.         To unregister this event, type the following: cscript smtpreg.vbs /remove 1 OnArrival SMTPDisclaimer

Test the event sink

To test your sink, send an e-mail message to an SMTP recipient who is external to your organization. The recipient should receive a modified message with the disclaimer added to the end of the message.

Note If you use a MAPI client such as Microsoft Outlook to send the e-mail, the recipient does not receive a modified message. This is because messages submitted using MAPI are not in SMTP format when the e-mail triggers the SMTP transport event. Therefore, changes that are made by the event's code are not persisted.

 Create the event sink using Visual Basic

1.       Create a new Microsoft Visual Basic ActiveX DLL. Name the project SMTPEventSink, and then name the module Disclaimer.

2.       Under Project References, add Microsoft CDO for Exchange 2000 Library, and then add Server Extension Objects COM Library.

3.    Put the following code in the module:

Dim TextDisclaimer As String
Dim HTMLDisclaimer As String

Implements IEventIsCacheable
Implements CDO.ISMTPOnArrival

Private Sub IEventIsCacheable_IsCacheable()
'Just returns S_OK.
End Sub

Private Sub Class_Initialize()
'TODO: Replace the sample disclaimer text with your own text.
TextDisclaimer = vbCrLf & "DISCLAIMER:" & vbCrLf & "Sample Disclaimer Text."
HTMLDisclaimer = "<p></p><p>DISCLAIMER:<br>Sample Disclaimer Text"
End Sub

Private Sub ISMTPOnArrival_OnArrival(ByVal Msg As CDO.IMessage, EventStatus As CDO.CdoEventStatus)
If Msg.HTMLBody "" Then
Dim szPartI As String
Dim szPartII As String
Dim pos As Integer

'Search for the "</body>" tag and insert the disclaimer before that tag.
pos = InStr(1, Msg.HTMLBody, "</body>", vbTextCompare)
szPartI = Left(Msg.HTMLBody, pos - 1)
szPartII = Right(Msg.HTMLBody, Len(Msg.HTMLBody) - (pos - 1))
Msg.HTMLBody = szPartI + HTMLDisclaimer + szPartII
End If

If Msg.TextBody "" Then
Msg.TextBody = Msg.TextBody & vbCrLf & TextDisclaimer & vbCrLf
End If

'Commit the content changes to the transport ADO Stream object.
Msg.DataSource.Save
EventStatus = cdoRunNextSink
End Sub

4.   Replace the sample disclaimer text with your own disclaimer text, and then build the DLL.

Register the event sink

1.      At a command prompt, locate the \Exchange SDK\SDK\Support\CDO\Scripts folder, and then type the following: cscript smtpreg.vbs /add 1 OnArrival SMTPDisclaimer SMTPEventSink.Disclaimer "mail from=*@your-domain-here.com [1]"

If this command succeeds, you receive a success message that is generated by the script.

2.        To unregister this event, type the following: cscript smtpreg.vbs /remove 1 OnArrival SMTPDisclaimer

Test the event sink

To test the event sink, send an e-mail message to an SMTP recipient who is external to your organization. The recipient receives a modified message with the disclaimer text added to the end of the message.

Note If you use a MAPI client, such as Microsoft Outlook, to send the e-mail message, the recipient does not receive a modified e-mail message. This is because the e-mail message that is submitted by using MAPI is not in an SMTP format when the e-mail message triggers the SMTP transport event. Therefore, changes that are made by the code of the event are not persisted.

Performance Notes

Based on tests from different sources, it shows that VBScript is slower than DLL (Visual Basic). However, both methods are very disk intensive and also VBScript eats more CPU

Third Party Products

There are plenty of third party solutions available today in the market, if you do not want to tweak with the inbuilt solutions. GFI MailEssentials, Exclaimer are few of them. You will find more on the internet if you search for them. Some products have additional functionality which you may or may not use it in your organization.

Additional Learning Resources & Useful Links.

273233 [2] PRB: CDOEX: Cannot change MAPI message contents in a CDO SMTP event sink

For additional information about managing event bindings, visit the following Microsoft Developer Network (MSDN) Web site:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cdosys/html/_cdosys_managing_event_bindings.asp [3]

 

 


Source URL: http://www.messagingtalk.org/content/125.html

Links:
[1] mailto:from=*@your-domain-here.com
[2] http://support.microsoft.com/kb/273233
[3] http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cdosys/html/_cdosys_managing_event_bindings.asp