Friday, October 20, 2006

Creating a Domain based auto response rule using rule.dll

Somebody asked last week about creating a personalized auto response message based on the sender domain. While this would be pretty easy to do with a normal store based event sink another option you can use is to create a server side rule using the rule.dll. The script to do this is pretty basic there are numerous samples that show how to create a rule that will process mail based on a string in the subject field. Well all you really need to do is take that same logic and apply it to the sender address instead. So you basically end up with a rule that does a substring on the sender address looking for in this case a particular email domain and then use the Reply action which creates a normal automatic reply email using the text you configure.

To use the script just configure the following line with the domain you want to use

importPropVal.Value = "@domain.com”

And this line with the text to respond with

objReplyMsg.Text = "Im Sorry this Mailbox isn't currently maned for after hour enquires please contact 2223-2222-222"

I’ve put a download copy of the script here the script itself looks like.

const SUBSTRING = 1 ' Substring
const IGNORECASE = &H00010000 ' Ignore case
const PR_SENDER_EMAIL_ADDRESS = &H0C1F001E
Const ACTION_REPLY = 4


servername = "servername"
mailboxname = "mailbox"

Set objSession = CreateObject("MAPI.Session")

objSession.Logon "","",false,true,true,true,servername & vbLF & mailboxname
Set objRules = CreateObject("MSExchange.Rules")
objRules.Folder = objSession.Inbox
Set objInbox = objSession.Inbox

Set CdoInfoStore = objSession.GetInfoStore
Set CdoFolderRoot = CdoInfoStore.RootFolder
Set CdoFolders = CdoFolderRoot.Folders

Set importPropVal = CreateObject("MSExchange.PropertyValue")
importPropVal.Tag = PR_SENDER_EMAIL_ADDRESS
importPropVal.Value = "@domain.com”

Set importPropCond = CreateObject("MSExchange.ContentCondition")
importPropCond.PropertyType = PR_SENDER_EMAIL_ADDRESS
importPropCond.Operator = SUBSTRING + IGNORECASE
importPropCond.Value = importPropVal

' 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 = "Im Sorry this Mailbox isn't currently maned for after hour enquires please contact 333-333-33"
objReplyMsg.Update


' Create action
Set objAction = CreateObject("MSExchange.Action")
objAction.ActionType = ACTION_REPLY
objAction.Arg = Array(objReplyMsg.ID,objReplyMsg.FolderID)

' Create new rule
Set objRule = CreateObject("MSExchange.Rule")
objRule.Name = "Domain Reply Rule"

' Add action and assign condition
objRule.Actions.Add , objAction
objRule.Condition = importPropCond

' 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