Platform SDK: Exchange Server |
Rules provide a mechanism so that you can manipulate messages in a Microsoft Exchange Information Store folder based on their contents or properties. For example, you can forward important messages containing specific text in the Subject field to a particular distribution list (DL) (for an introduction to rules, see About Rules). Rules created using the Rule component are server-side, and execute when messages or items are present in folders in the Microsoft Exchange Information Store.
To create a rule, you must create at least one Action and one Condition. When a message arrives that satisfies the Rule's condition, the actions are applied.
For example, suppose you want to delete any message that has the word "delete" in the Subject field. You create the following rule for the TestUser account on the Microsoft Exchange server SERVER:
const SUBSTRING = 1 ' Substring const IGNORECASE = &H00010000 ' Ignore case const ACTION_DELETE = 3 const CdoPR_SUBJECT = &H0037001E ' Create PropertyValue with delete in the Subject Set objPropVal = CreateObject("MSExchange.PropertyValue") objPropVal.Tag = CdoPR_SUBJECT objPropVal.Value = "delete" ' Create ContentCondition Set objContCond = CreateObject("MSExchange.ContentCondition") objContCond.PropertyType = CdoPR_SUBJECT objContCond.Operator = SUBSTRING + IGNORECASE objContCond.Value = objPropVal ' Create action Set objAction = CreateObject("MSExchange.Action") objAction.ActionType = ACTION_DELETE ' Create Rule object Set objRule = CreateObject("MSExchange.Rule") objRule.Name = "Delete Test" objRule.Condition = objContCond objRule.Actions.Add ,objAction ' Create session object and logon to machine SERVER as TestUser Set objCDOSess = CreateObject("MAPI.Session") objCDOSess.Logon "","",false,true,true,true,"SERVER" & vbLF & "TestUser" ' Create Rules object and add new Rule to Inbox rules Set objRules = CreateObject("MSExchange.Rules") objRules.Folder = objCDOSess.Inbox objRules.Add , objRule ' Update the rules objRules.Update objCDOSess.Logoff Set objPropVal = Nothing Set objContCond = Nothing Set objAction = Nothing Set objRule = Nothing Set objCDOSess = Nothing Set objRules = Nothing
To create an action you set the two properties IAction::ActionType and IAction::Arg. Set the ActionType property to one of the enumerated types. These include:
Name | Value | Description |
---|---|---|
ACTION_MOVE | 1 | Move the message to folder specified in Arg |
ACTION_DELETE | 3 | Delete the message |
ACTION_REPLY | 4 | Respond to the message with the message specified in Arg |
ACTION_FORWARD | 6 | Forward the message to the recipient list specified in Arg |
Set the Arg property to match the requirements of the ActionType property. For example, if the ActionType is ACTION_FORWARD, the Arg property is a SAFEARRAY of BSTR values (array of strings) representing one or more addresses.
Here's an example of an action that moves messages to the "Deleted Items" folder.
const ACTION_MOVE = 1 Set objSession = CreateObject("MAPI.Session") objSession.Logon "","",false,true,true,true,"SERVER" & vbLF & "TestUser" ' Display all private folders Set objInfoStore = objSession.InfoStores (2) Set objPrivateFs = objInfoStore.RootFolder.Folders For Each objFolder in objPrivateFs If objFolder.Name = "Deleted Items" Then ActionFolder = objFolder Exit For End If Next Set objAction = CreateObject("MSExchange.Action") objAction.ActionType = ACTION_MOVE objAction.Arg = ActionFolder ...
Here's an example of an action that forwards messages to the user Fred.
const CdoAddressListGAL = 0 ' Global address list const CdoAddressListPAB = 1 ' Personal address book const CdoPR_SEARCH_KEY = &H300B0102 const ACTION_FORWARD = 6 ' Create the rules object Set objRules = CreateObject("MSExchange.Rules") ' Create the CDO MAPI session Set objSession = CreateObject("MAPI.Session") ' Logon to \\SERVER as TestUser objSession.Logon "","",False,True,True,True,"SERVER" & vbLF & "TestUser" ' Get the Rules collection on TestUser's Inbox objRules.Folder = objSession.Inbox ' Get Address Entries for Global Address list (GAL) Set objGAL = objSession.GetAddressList(CdoAddressListGAL) Set objGALEntries = objGAL.AddressEntries ' Search for Fred For Each objAddress In objGALEntries If objAddress.Name = "Fred" Then ActionAddressList(0) = ObjAddress.ID Exit For End If Next Set objAction = CreateObject("MSExchange.Action") objAction.ActionType = ACTION_FORWARD objAction.Arg = ActionAddressList ...
This section describes how to determine what condition to use based on the attributes of a message. This section is not exhaustive. There are over 400 CDO properties of which perhaps 300 are properties that you might want to use in a condition. Therefore, this section discusses only the following message attributes:
Use the PropertyCondition object to identify messages based on whether your name appears in the To or Cc field.
Here's an example of a rule that deletes messages where your name appears in the Cc line.
const CdoPR_MESSAGE_CC_ME = &H0058000B const REL_EQ = 7 const ACTION_DELETE = 3 ' Create the rules object Set objRules = CreateObject("MSExchange.Rules") ' Set Property Value Set objPropVal = CreateObject("MSExchange.PropertyValue") objPropVal.Tag = CdoPR_MESSAGE_CC_ME objPropVal.Value = True ' Set Property Condition Set objPropCond = CreateObject("MSExchange.PropertyCondition") objPropCond.Operator = REL_EQ objPropCond.PropertyTag = CdoPR_MESSAGE_CC_ME objPropCond.Value = objPropVal ' Set Action Set objAction = CreateObject("MSExchange.Action") objAction.ActionType = ACTION_DELETE ' Add Action and assign Condition Set objRule = CreateObject("MSExchange.Rule") objRule.Name = "Cc Test" objRule.Actions.Add , objAction objRule.Condition = objPropCond ' Create the CDO MAPI session Set objSession = CreateObject("MAPI.Session") ' Logon to \\SERVER as TestUser objSession.Logon "","",False,True,True,True,"SERVER" & vbLF & "TestUser" ' Get the Rules on TestUser's Inbox objRules.Folder = objSession.Inbox ' Add the rule objRules.Add ,objRule ' Save the changes in the Information Store objRules.Update ' Logoff the MAPI session objSession.Logoff ' Relese the objects we created Set objRules = Nothing Set objSession = Nothing Set objRule = Nothing Set objPropVal = Nothing Set objPropCond = Nothing Set objAction = Nothing
Use the CommentCondition object to identify messages based on whether a specific user's name appears in the From field.
Here's an example of a rule that deletes a message sent from the personal address book entry Spammer. It saves the sender's entry ID in a Comment condition, in case you later want to display further information about the sender in your script.
const CdoAddressListGAL = 0 ' Global address list const CdoAddressListPAB = 1 ' Personal address book const CdoPR_SENDER_ENTRYID = &H0C190102 const CdoPR_SEARCH_KEY = &H300B0102 const CdoPR_SENDER_SEARCH_KEY = &H0C1D0102 const REL_EQ = 7 const ACTION_DELETE = 3 ' Create the rules object Set objRules = CreateObject("MSExchange.Rules") ' Create the CDO MAPI session Set objSession = CreateObject("MAPI.Session") ' Logon to \\SERVER as TestUser objSession.Logon "","",False,True,True,True,"SERVER" & vbLF & "TestUser" ' Get the Rules collection on TestUser's Inbox objRules.Folder = objSession.Inbox ' Get Address Entries for Personal Address Book (PAB) Set objPAB = objSession.GetAddressList(CdoAddressListPAB) Set objPABEntries = objPAB.AddressEntries Found = False ' Search PAB entries for Spammer For Each objFromAddress In objPABEntries If objFromAddress.Name = "Spammer" Then ' Get search key of the address entry SenderSearchKey = objFromAddress.Fields(CdoPR_SEARCH_KEY) Found = True Exit For End If Next If NOT Found Then Wscript.Echo "Address for Spammer not found!" Wscript.Quit End If ' Create a rule for Inbox Set objRule = CreateObject("MSExchange.Rule") objRule.Name = "From Spammer Test" ' Get entry ID of address entry Set objIDPropVal = CreateObject("MSExchange.PropertyValue") objIDPropVal.Tag = CdoPR_SENDER_ENTRYID objIDPropVal.Value = objFromAddress.ID ' Set Property Value with search key for Property Condition Set objSearchPropVal = CreateObject("MSExchange.PropertyValue") objSearchPropVal.Tag = CdoPR_SENDER_SEARCH_KEY objSearchPropVal.Value = SenderSearchKey ' Set Property Condition Set objPropCond = CreateObject("MSExchange.PropertyCondition") objPropCond.Operator = REL_EQ objPropCond.PropertyTag = CdoPR_SENDER_SEARCH_KEY objPropCond.Value = objSearchPropVal ' Set the comment condition Set objCmtCond = CreateObject("MSExchange.CommentCondition") objCmtCond.Condition = objPropCond objCmtCond.Add objIDPropVal ' Set Action Set objAction = CreateObject("MSExchange.Action") objAction.ActionType = ACTION_DELETE objRule.Actions.Add , objAction ' Assign Condition objRule.Condition = objCmtCond ' Add the rule objRules.Add ,objRule ' Commit the change objRules.Update ' Logoff the MAPI session objSession.Logoff ' Relese the objects we created Set objRules = Nothing Set objSession = Nothing Set objPAB = Nothing Set objPABEntries = Nothing Set objFromAddress = Nothing Set objRule = Nothing Set objIDPropVal = Nothing Set objSearchPropVal = Nothing Set objPropCond = Nothing Set objCmtCond = Nothing Set objAction = Nothing
Use the ContentCondition object to identify messages based on text in the Subject field or body of the message.
Here's an example of a rule that deletes a message with the word delete in the Subject field.
const SUBSTRING = 1 const IGNORECASE = &H00010000 const ACTION_DELETE = 3 const CdoPR_BODY = &H1000001E const CdoPR_SUBJECT = &H0037001E ' Create PropVal Set objPropVal = CreateObject("MSExchange.PropertyValue") objPropVal.Tag = CdoPR_SUBJECT objPropVal.Value = "delete" ' Create ContentCondition Set objContCond = CreateObject("MSExchange.ContentCondition") objContCond.PropertyType = CdoPR_SUBJECT objContCond.Operator = SUBSTRING + IGNORECASE objContCond.Value = objPropVal ' Create action Set objAction = CreateObject("MSExchange.Action") objAction.ActionType = ACTION_DELETE ' Create Rule object Set objRule = CreateObject("MSExchange.Rule") objRule.Name = "Subject Test" ' Set condition and add action objRule.Condition = objContCond objRule.Actions.Add ,objAction ' Create session object and logon Set objCDOSess = CreateObject("MAPI.Session") objCDOSess.Logon "","",false,true,true,true,"SERVER" & vbLF & "TestUser" ' Create Rules object and add new Rule to Inbox collection Set objRules = CreateObject("MSExchange.Rules") objRules.Folder = objCDOSess.Inbox objRules.Add objRules.Count + 1, objRule ' Update the collection and cleanup objRules.Update objCDOSess.Logoff Set objRules = Nothing Set objCDOSess = Nothing Set objRule = Nothing Set objContCond = Nothing Set objPropVal = Nothing Set objAction = Nothing
Use the PropertyCondition object to identify messages based on its Importance.
Here's an example of a rule that deletes messages of low importance.
const REL_EQ = 7 const ACTION_DELETE = 3 const CdoPR_IMPORTANCE = &H00170003 const IMPORTANCE_LOW = 0 ' Create PropVal for messages with low importance Set importPropVal = CreateObject("MSExchange.PropertyValue") importPropVal.Tag = CdoPR_IMPORTANCE importPropVal.Value = IMPORTANCE_LOW ' Create property condition for when the importance is low Set importPropCond = CreateObject("MSExchange.PropertyCondition") importPropCond.PropertyTag = CdoPR_IMPORTANCE importPropCond.Operator = REL_EQ importPropCond.Value = importPropVal ' Create action Set objAction = CreateObject("MSExchange.Action") objAction.ActionType = ACTION_DELETE ' Create new rule Set objRule = CreateObject("MSExchange.Rule") objRule.Name = "Importance Test" ' Add action and assign condition objRule.Actions.Add , objAction objRule.Condition = importPropCond ' Open MAPI session and log on Set objSession = CreateObject("MAPI.Session") ' Log onto \\SERVER as TestUser objSession.Logon "","",false,true,true,true,"SERVER" & vbLF & "TestUser" ' Get Rules Set objRules = CreateObject("MSExchange.Rules") ' Open TestUser's inbox objRules.Folder = objSession.Inbox ' Add rule and update objRules.Add , objRule objRules.Update ' Log off and cleanup objSession.Logoff Set objRules = Nothing Set objSession = Nothing Set importProp = Nothing Set importPropVal = Nothing Set objAction = Nothing Set objRule = Nothing
Use the PropertyCondition object to identify messages based on its Sensitivity level.
Here's an example of a rule that deletes messages marked company confidential.
const REL_EQ = 7 const ACTION_DELETE = 3 const CdoPR_SENSITIVITY = &H00360003 const SENSITIVITY_COMPANY_CONFIDENTIAL = 3 ' Create PropVal for messages marked company confidential Set importPropVal = CreateObject("MSExchange.PropertyValue") importPropVal.Tag = CdoPR_SENSITIVITY importPropVal.Value = SENSITIVITY_COMPANY_CONFIDENTIAL ' Create property condition for when the message is marked company confidential Set importPropCond = CreateObject("MSExchange.PropertyCondition") importPropCond.PropertyTag = CdoPR_SENSITIVITY importPropCond.Operator = REL_EQ importPropCond.Value = importPropVal ' Create action Set objAction = CreateObject("MSExchange.Action") objAction.ActionType = ACTION_DELETE ' Create new rule Set objRule = CreateObject("MSExchange.Rule") objRule.Name = "Sensitivity Test" ' Add action and assign condition objRule.Actions.Add , objAction objRule.Condition = importPropCond ' Open MAPI session and log on Set objSession = CreateObject("MAPI.Session") ' Log onto \\SERVER as TestUser objSession.Logon "","",false,true,true,true,"SERVER" & vbLF & "TestUser" ' Get Rules Set objRules = CreateObject("MSExchange.Rules") ' Open TestUser's inbox objRules.Folder = objSession.Inbox ' Add rule and update objRules.Add , objRule objRules.Update ' Log off and cleanup objSession.Logoff Set objRules = Nothing Set objSession = Nothing Set importProp = Nothing Set importPropVal = Nothing Set objAction = Nothing Set objRule = Nothing
Use the BitmaskCondition object to identify messages with attachments.
Here's an example of a rule that deletes messages with attachments.
const ACTION_DELETE = 3 const CdoPR_MESSAGE_FLAGS = &H0E070003 const MSGFLAG_HASATTACH = &H10 const B_NEZ = 2 ' Create property condition for when the message has attachments Set BitMaskPropCond = CreateObject("MSExchange.PropertyCondition") BitMaskPropCond.PropertyTag = CdoPR_MESSAGE_FLAGS BitMaskPropCond.Operator = B_NEZ BitMaskPropCond.Value = MSGFLAG_HASATTACH ' Create action Set objAction = CreateObject("MSExchange.Action") objAction.ActionType = ACTION_DELETE ' Create new rule Set objRule = CreateObject("MSExchange.Rule") objRule.Name = "Attachment Test" ' Add action and assign condition objRule.Actions.Add , objAction objRule.Condition = BitMaskPropCond ' Open MAPI session and log on Set objSession = CreateObject("MAPI.Session") ' Log onto \\SERVER as TestUser objSession.Logon "","",false,true,true,true,"SERVER" & vbLF & "TestUser" ' Get Rules Set objRules = CreateObject("MSExchange.Rules") ' Open TestUser's inbox objRules.Folder = objSession.Inbox ' Add rule and update objRules.Add , objRule objRules.Update ' Log off and cleanup objSession.Logoff Set objRules = Nothing Set objSession = Nothing Set importProp = Nothing Set importPropVal = Nothing Set objAction = Nothing Set objRule = Nothing
Use the PropertyCondition object to identify messages based on when the message was sent.
Here's an example of a rule that deletes messages sent before midnight.
const REL_LE = 3 const ACTION_DELETE = 3 const CdoPR_CREATION_TIME = &H30070040 Midnight = CDate("9/14/98 0:00:00") ' Create PropVal for messages sent before midnight Set CreatePropVal = CreateObject("MSExchange.PropertyValue") CreatePropVal.Tag = CdoPR_CREATION_TIME CreatePropVal.Value = Midnight ' Create property condition for when the message was sent before midnight Set CreatePropCond = CreateObject("MSExchange.PropertyCondition") CreatePropCond.PropertyTag = CdoPR_CREATION_TIME CreatePropCond.Operator = REL_LE CreatePropCond.Value = CreatePropVal ' Create action Set objAction = CreateObject("MSExchange.Action") objAction.ActionType = ACTION_DELETE ' Create new rule Set objRule = CreateObject("MSExchange.Rule") objRule.Name = "Sensitivity Test" ' Add action and assign condition objRule.Actions.Add , objAction objRule.Condition = CreatePropCond ' Open MAPI session and log on Set objSession = CreateObject("MAPI.Session") ' Log onto \\SERVER as TestUser objSession.Logon "","",false,true,true,true,"SERVER" & vbLF & "TestUser" ' Get Rules Set objRules = CreateObject("MSExchange.Rules") ' Open TestUser's inbox objRules.Folder = objSession.Inbox ' Add rule and update objRules.Add , objRule objRules.Update ' Log off and cleanup objSession.Logoff Set objRules = Nothing Set objSession = Nothing Set importProp = Nothing Set importPropVal = Nothing Set objAction = Nothing Set objRule = Nothing
Use the SizeCondition object to identify messages based on the size of a property.
Here's an example of a rule that deletes messages whose Body property is greater than 1Mbyte.
const REL_GT = 2 const ACTION_DELETE = 3 const CdoPR_BODY = &H1000001E ' Create property condition for when the message property is greater than SIZE Set SizeProp = CreateObject("MSExchange.SizeCondition") SizeProp.PropertyTag = CdoPR_BODY SizeProp.Operator = REL_GT SizeProp.Size = &H100000 ' Create action Set objAction = CreateObject("MSExchange.Action") objAction.ActionType = ACTION_DELETE ' Create new rule Set objRule = CreateObject("MSExchange.Rule") objRule.Name = "Size Test" ' Add action and assign condition objRule.Actions.Add , objAction objRule.Condition = SizeProp ' Open MAPI session and log on Set objSession = CreateObject("MAPI.Session") ' Log onto \\SERVER as TestUser objSession.Logon "","",false,true,true,true,"SERVER" & vbLF & "TestUser" ' Get Rules Set objRules = CreateObject("MSExchange.Rules") ' Open TestUser's inbox objRules.Folder = objSession.Inbox ' Add rule and update objRules.Add , objRule objRules.Update ' Log off and cleanup objSession.Logoff Set objRules = Nothing Set objSession = Nothing Set importProp = Nothing Set importPropVal = Nothing Set objAction = Nothing Set objRule = Nothing
The major effort in creating a rule is building a condition. It's trivial to create simple (one condition object) conditions. For example, to trap messages that are greater than 50Kbytes in size, use SizeCondition. It's when you need to create a condition with multiple condition objects that things get interesting.
To save yourself a lot of grief, try first drawing a logic tree. For example, to specify a condition where mail sent to you, from MyLittleBrother, and marked low importance, you might create the following diagram:
You typically build a condition object from the bottom up. In this example:
You can see the code that implements this condition in the Rule class example.
To create a ContentCondition, perform the following steps:
For example, to identify messages where the Subject field contains the string "Test":
const SUBSTRING = 1 Set myPropVal = CreateObject("MSExchange.PropVal") myPropVal.Tag = CdoPR_SUBJECT myPropVal.Value = "Test" Set myPropCond = CreateObject("MSExchange.PropertyCondition") myPropCond.Value = myPropVal myPropCond.PropertyType = CdoPR_SUBJECT myPropCond.Operator = SUBSTRING
To create a Property condition, perform the following steps:
For example, to identify messages sent to you:
const CdoPR_MESSAGE_TO_ME = &H0057000B const REL_EQ = 7 Set myPropVal = CreateObject("MSExchange.PropVal") myPropVal.Tag = CdoPR_MESSAGE_TO_ME myPropVal.Value = TRUE Set myPropCond = CreateObject("MSExchange.PropertyCondition") myPropCond.Operator = REL_EQ myPropCond.PropertyTag = CdoPR_MESSAGE_TO_ME myPropCond.Value = myPropVal
To create a ComparePropsCondition, perform the following steps:
For example, to identify messages where the sensitivity level is higher than what the original sender specified:
const CdoPR_ORIGINAL_SENSITIVITY = &H002E0003 const CdoPR_SENSITIVITY = &H00360003 const REL_LT = 4 Set myCmpPropCond = CreateObject("MSExchange.ComparePropsCondition") myCmpPropCond.Operator = REL_LT myCmpPropCond.PropertyTag1 = CdoPR_ORIGINAL_SENSITIVITY myCmpPropCond.PropertyTag2 = CdoPR_SENSITIVITY
To create a BitmaskCondition, perform the following steps:
For example, to identify messages with attachments:
const CdoPR_MESSAGE_FLAGS = &H0E070003 const MSGFLAG_HASATTACH = &H10 const B_NEZ = 2 Set myPropVal = CreateObject("MSExchange.PropertyValue") myPropVal.Tag = CdoPR_MESSAGE_FLAGS myPropVal.Value = MSGFLAG_HASATTACH Set BitMaskPropCond = CreateObject("MSExchange.PropertyCondition") BitMaskPropCond.PropertyTag = CdoPR_MESSAGE_FLAGS BitMaskPropCond.Operator = B_NEZ BitMaskPropCond.Value = myPropVal
To create a SizeCondition, perform the following steps:
For example, to identify messages whose body size is greater than 1 MBtye:
const REL_GT = 2 const CdoPR_BODY = &H1000001E const SIZE = &H100000 Set SizeCond = CreateObject("MSExchange.SizeCondition") SizeCond.PropertyTag = CdoPR_BODY SizeCond.Operator = REL_GT SizeCond.Size = SIZE
To create a SubCondition, perform the following steps:
For example, to identify messages with attachments named junk.gif:
const CdoPR_ATTACH_FILENAME = &H3704001E const REL_EQ = 7 const MSG_ATTACH = 2 Set objPropVal = CreateObject("MSExchange.PropertyValue") objPropVal.Tag = CdoPR_ATTACH_FILENAME objPropVal.Value = "junk.gif" Set objPropCond = CreateObject("MSExchange.PropertyCondition") objPropCond.Value = objPropVal objPropCond.Operator = REL_EQ objPropCond.PropertyTag = CdoPR_ATTACH_FILENAME Set objSubCond = CreateObject("MSExchange.SubCondition") objSubCond.Condition = objPropCond objSubCond.Operator = MSG_ATTACH ...
To create an ExistsCondition, perform the following steps:
For example, to identify messages where the CdoPR_ACCOUNT property (&H3A00001E) is set:
const CdoPR_ACCOUNT = &H3A00001E Set objExistsCond = CreateObject("MSExchange.ExistsCondition") objExistsCond.PropertyTag = CdoPR_ACCOUNT ...
To create a LogicalCondition, perform the following steps:
For example, to identify messages marked as low importance from Fred:
const CdoPR_SENDER_NAME = &H0C1A001E const REL_EQ = 7 const CdoPR_IMPORTANCE = &H00170003 const IMPORTANCE_LOW = 0 const L_AND = 1 Set fromPropVal = CreateObject("MSExchange.PropertyValue") fromPropVal.Tag = CdoPR_SENDER_NAME fromPropVal.Value = "Fred" Set fromPropCond = CreateObject("MSExchange.PropertyCondition") fromPropCond.Value = fromPropVal fromPropCond.Operator = REL_EQ fromPropCond.PropertyTag = CdoPR_SENDER_NAME Set importPropVal = CreateObject("MSExchange.PropertyValue") importPropVal.Tag = CdoPR_IMPORTANCE importPropVal.Value = IMPORTANCE_LOW Set importPropCond = CreateObject("MSExchange.PropertyCondition") importPropCond.PropertyTag = CdoPR_IMPORTANCE importPropCond.Operator = REL_EQ importPropCond.Value = importPropVal Set logPropCond = CreateObject("MSExchange.LogicalCondition") logCond.Operator = L_AND logCond.Add , importPropCond logCond.Add , fromPropCond ...
Once you've created your actions and condition, add the actions to your new rule using the Rule.Actions.Add method, and assign the condition to the rule by setting the IRule::Condition property to the condition object. Don't forget to call the Rules collection's IRules::Update method to make your changes permanent.