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 = "”

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

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.Value = "”

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"

' 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

' Log off and cleanup

Set objRules = Nothing
Set objSession = Nothing
Set importProp = Nothing
Set importPropVal = Nothing
Set objAction = Nothing
Set objRule = Nothing


Michael M ST. Pete USA said...

This certainly looks like a great solution. Would it be possible to apply this to an e-mail enabled distribution group? We have a group that wants to send an auto response when an e-mail is sent to them.

Glen said...

A distribution group has no mailbox associated with it so you cant create a rule for a distribution list but what you can do is create a public folder (or a mailbox) add this as a member of the distribution list. And then setup your Auto-response rule on that mailbox or public folder. There for any mail sent to the list when that mailbox or public folder receives a copy it will trigger the auto-response rule.

briac said...

Hi Glen,

Thanks for this script it works great. One thing I would like to ask you. I noticed that if a user creates a rule, for instance an auto reply rule with OOF assistant, and I run my script to delete my previously created rules (created with rule.dll) my script fails with a Permission Denied, if I open Mapi editor I can see the rule created with Out Of Office, I remove it and then my ‘delete-rule’ script runs fine.

Is there any trick to avoid this? I’d love being able to directly remove programmatically all message class : IPM.Note.Rules.OofTemplate.Microsoft
Do you know a way to achieve this ?

Glen said...

What version of Exchange are you using ? I would suggest using RDO as you can access the Rule table directly so its important to delete both the entry from the rules table and the Item itself when removing a rule object from Exchange. If you using 2010 you can now do this all with EWS.


anand singh said...

will it work for o365?