Microsoft KB Archive/251125

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

Article ID: 251125

Article Last Modified on 11/3/2005

-

APPLIES TO


 * Microsoft Collaboration Data Objects 1.21
 * Microsoft Visual Basic 6.0 Enterprise Edition
 * 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++ 6.0 Standard Edition

-



This article was previously published under Q251125



SUMMARY
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.



MORE INFORMATION
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:  In Visual Basic, create a new Standard EXE project. 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.)  Copy the following function to the code module of the default form: Option Explicit Const SUBJECT_TO_SEARCH = "Test" Public Function AddReplyAndDeleteRule(ByVal strProfileInfo As String) As Long

Dim 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.Rule Dim objDeleteRule As MSExchange.Rule Dim objPropVal As MSExchange.PropVal            ' Property value. Dim objContCond As MSExchange.ContentCondition  ' Condition. Dim objReplyAction As MSExchange.Action Dim objDeleteAction As MSExchange.Action

Const SUBSTRING = 1 Const IGNORECASE = &H10000 Const ACTION_DELETE = 3 Const ACTION_REPLY = 4 Const CdoPR_SUBJECT = &H37001E

On Error GoTo Error_Handler

' Create and log on to session object. Set objCDOSession = New MAPI.Session objCDOSession.Logon, , False, True, True, True, strProfileInfo Set objInbox = objCDOSession.Inbox

'Each folder has a Rules collection. Set colRules = New MSExchange.Rules colRules.Folder = objInbox

' Create condition for rules. Set objPropVal = New MSExchange.PropVal objPropVal.Tag = CdoPR_SUBJECT 'looks in Subject property objPropVal.Value = SUBJECT_TO_SEARCH 'looks for this string

' Create ContentCondition Set objContCond = New MSExchange.ContentCondition objContCond.Value = objPropVal objContCond.PropertyType = CdoPR_SUBJECT objContCond.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.Action objReplyAction.ActionType = ACTION_REPLY

Dim vntArgs(1) As Variant vntArgs(0) = objReplyMsg.ID      ' ID of template vntArgs(1) = objReplyMsg.FolderID ' folder ID of template objReplyAction.Arg = vntArgs     ' Set the args

' Set other reply rule properties. objReplyRule.Name = "Reply Test" objReplyRule.Condition = objContCond objReplyRule.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.Action objDeleteAction.ActionType = ACTION_DELETE

' Set delete rule properties. objDeleteRule.Name = "Delete Test" objDeleteRule.Condition = objContCond 'reuse condition objDeleteRule.Actions.Add, objDeleteAction colRules.Add, objDeleteRule colRules.Update

' Wrap up. objCDOSession.Logoff AddReplyAndDeleteRule = 0 'successful

Set objPropVal = Nothing Set objContCond = Nothing Set objReplyAction = Nothing Set objDeleteAction = Nothing Set objReplyRule = Nothing Set objDeleteRule = Nothing Set objReplyMsg = Nothing Set colRules = Nothing Set objInbox = Nothing Set objCDOSession = Nothing

Exit Function

Error_Handler: AddReplyAndDeleteRule = Err.Number

End Function   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  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. 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.</ol>

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.

<div class="moreinformation_section">

To download the Microsoft Exchange Development Kit (EDK) 5.5, visit the following Microsoft Web site:

http://www.microsoft.com/downloads/details.aspx?FamilyId=36A309C3-8C55-4476-8785-CAFC59A2D075&displaylang=en

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

<div class="references_section">