How to use the Rule.dll sample to create an inbox rule in Visual Basic

This article was previously published under Q251125
This article has been archived. It is offered "as is" and will no longer be updated.
This article describes how to use the Rule.dll sample to add a set of rules to the Inbox of a mailbox. Rules are a powerful technique that can be applied to many mailboxes without the drawbacks associated with server event scripts.
The rules that are added in this sample function review all incoming messages. If the message subject includes the word "test", a reply message is sent and the original message is deleted.

The Platform Software Development Kit (SDK) includes a Rules COM component. The documentation in the SDK provides examples of using the Rules component in the context of an Active Server Pages (ASP) application. You can, however, also use this sample in applications using the Collaborative Data Objects (CDO) library.

Note The Rule.dll must be compiled before use. Compiling the Rule.dll library requires skill in Visual C++ and is beyond the scope of this article. See the documentation listed in the "References" section of this article for more information.

This function creates three items in the HiddenMessages collection of the Inbox folder:
  • The reply rule
  • The template for the reply message
  • The delete rule
To run this sample program, follow these steps:
  1. In Visual Basic, create a new Standard EXE project.
  2. Add a reference to the Microsoft CDO 1.21 Library, and to the Rule.dll library. (You will need to browse to the location where you built Rule.dll.)
  3. Copy the following function to the code module of the default form:
    Option ExplicitConst SUBJECT_TO_SEARCH = "Test"Public Function AddReplyAndDeleteRule(ByVal strProfileInfo As String) As LongDim objCDOSession As MAPI.Session                ' CDO Session.Dim objInbox As MAPI.Folder                      ' Folder to set rule.Dim colRules As MSExchange.Rules                 ' Rules collection.Dim objReplyMsg As MAPI.Message                  ' Reply template.Dim objReplyRule As MSExchange.RuleDim objDeleteRule As MSExchange.RuleDim objPropVal As MSExchange.PropVal             ' Property value.Dim objContCond As MSExchange.ContentCondition   ' Condition.Dim objReplyAction As MSExchange.ActionDim objDeleteAction As MSExchange.ActionConst SUBSTRING = 1Const IGNORECASE = &H10000Const ACTION_DELETE = 3Const ACTION_REPLY = 4Const CdoPR_SUBJECT = &H37001EOn Error GoTo Error_Handler' Create and log on to session object.Set objCDOSession = New MAPI.SessionobjCDOSession.Logon , , False, True, True, True, strProfileInfoSet objInbox = objCDOSession.Inbox'Each folder has a Rules collection.Set colRules = New MSExchange.RulescolRules.Folder = objInbox' Create condition for rules.Set objPropVal = New MSExchange.PropValobjPropVal.Tag = CdoPR_SUBJECT  'looks in Subject propertyobjPropVal.Value = SUBJECT_TO_SEARCH 'looks for this string' Create ContentConditionSet objContCond = New MSExchange.ContentConditionobjContCond.Value = objPropValobjContCond.PropertyType = CdoPR_SUBJECTobjContCond.Operator = SUBSTRING + IGNORECASE' Previous line ignores case and other text.' Create reply message and store in HiddenMessages collection.Set objReplyMsg = objInbox.HiddenMessages.Add' Set reply message properties.objReplyMsg.Type = "IPM.Note.Rules.ReplyTemplate.Microsoft"objReplyMsg.Text = "Please do not send messages to this address"objReplyMsg.Update' Create Reply Rule first.Set objReplyRule = New MSExchange.Rule' Set reply action.Set objReplyAction = New MSExchange.ActionobjReplyAction.ActionType = ACTION_REPLYDim vntArgs(1) As VariantvntArgs(0) = objReplyMsg.ID       ' ID of templatevntArgs(1) = objReplyMsg.FolderID ' folder ID of templateobjReplyAction.Arg = vntArgs      ' Set the args' Set other reply rule properties.objReplyRule.Name = "Reply Test"objReplyRule.Condition = objContCondobjReplyRule.Actions.Add , objReplyAction' Add reply rule to rules collection.colRules.Add , objReplyRule' Create Delete Rule object.Set objDeleteRule = New MSExchange.Rule' Create delete action.Set objDeleteAction = New MSExchange.ActionobjDeleteAction.ActionType = ACTION_DELETE' Set delete rule properties.objDeleteRule.Name = "Delete Test"objDeleteRule.Condition = objContCond  'reuse conditionobjDeleteRule.Actions.Add , objDeleteActioncolRules.Add , objDeleteRulecolRules.Update' Wrap up.objCDOSession.LogoffAddReplyAndDeleteRule = 0  'successfulSet objPropVal = NothingSet objContCond = NothingSet objReplyAction = NothingSet objDeleteAction = NothingSet objReplyRule = NothingSet objDeleteRule = NothingSet objReplyMsg = NothingSet colRules = NothingSet objInbox = NothingSet objCDOSession = NothingExit FunctionError_Handler:AddReplyAndDeleteRule = Err.NumberEnd Function
  4. Add a command button to the default form, and paste the following code to the button click event procedure (note that you must change the ServerName and MailBox to the appropriate names):
    Dim lResult As Long' TODO Change the ServerName and MailBox to the appropriate names.lResult = AddReplyAndDeleteRule("ServerName" & vbLf & "MailBox")MsgBox "Call to function: " & lResult
  5. Run the project and then click the command button. If the message box displays a "0" (zero), the process worked and the rules were added; otherwise, an error value is displayed.
  6. Using Microsoft Outlook or another mail client, send a message with the subject "Test" to the mailbox. The rules on the mailbox will reply back and delete the message.
Note The rules that are added by the Rule.dll COM component do not appear in the Outlook user interface; the only way to manipulate them is through the code. This is a limitation of the COM interfaces used to create rules, not a limitation of the sample itself. Therefore, it is recommended that all testing be done on a mailbox that you create for this purpose.
To download the Microsoft Exchange Development Kit (EDK) 5.5, visit the following Microsoft Web site:Microsoft no longer supports Microsoft EDK 5.5 with versions of Microsoft Exchange Server later than 5.5.For more information, click the following article number to view the article in the Microsoft Knowledge Base:
815365 The Microsoft EDK 5.5 is not supported with versions of Microsoft Exchange Server later than 5.5
For more information, see the following Microsoft Developer Network (MSDN) topics:

Article ID: 251125 - Last Review: 02/28/2014 00:30:29 - Revision: 5.3

Microsoft Collaboration Data Objects 1.21, Microsoft Visual Basic Enterprise Edition for Windows 6.0, Microsoft Exchange Server 2003 Software Development Kit, Microsoft Exchange Server 5.5 Standard Edition, Microsoft Visual C++ 6.0 Enterprise Edition, Microsoft Visual C++ 6.0 Professional Edition, Microsoft Visual C++, 32-bit Learning Edition 6.0

  • kbnosurvey kbarchive kbhowto KB251125