Home : Working with Rules in the Exchange Server
Q137618 - HOWTO: Working with Rules in the Exchange Server

Working with Rules in the Exchange Server


Microsoft Exchange uses "rules" to define actions that get triggered when certain conditions are met. For instance, you can set up a rule such that when a new message arrives in your inbox with the subject containing the phrase "want more money?", this message automatically gets deleted and a reply message sent to the sender with the subject "Please don't send me any more spam!".

Rules can be accessed programmatically through the COM library RULE.DLL which comes with the Exchange 5.5 SDK. RULE.DLL is only on version 1 but it works with Exchange Server 5.5, 2000 and 2003. You can use Java via J-Integra to programmatically create rules in the Exchange Server. To do this, follow these steps:

  1. Setup a Windows machine with Outlook installed in it. We will call this the client machine.
  2. Install CDO.DLL in the client machine. To do this, refer to KB article 113792.
  3. Obtain a copy of RULE.DLL. You can download one from here: http://www.cdolive.com/download/ruleasp.zip
  4. Register RULE.DLL in the client machine by using regsvr32
  5. Compile and run the sample Java code below. This sample Java application needs the Java proxies for RULE.DLL (rule.jar) which you can download here. You will also need the J-Integra for Exchange runtime (jintegra.jar) and the Java proxies for CDO (cdo.jar), both of which are found in the "library" folder when you download J-Integra for Exchange.
import com.intrinsyc.cdo.*;
import com.intrinsyc.rule.*;

public class InboxRule {

    static String domain     = "DOMAIN";    //domain where the CDO host machine belongs
                                            //make sure it's all capitalized
    static String user       = "username";  //username of Java-Exchange user
    static String password   = "password";  //password of Java-Exchange user
    static String mailbox    = "mailbox";   //mailbox of Java-Exchange user. By MS Exchange settings,
                                            //the mailbox name is the same as the username
    static String CDOmachine = "";   // IP address of the CDO host machine
                                                    // or name of the machine (e.g. "machine1")
    static String exchangeServer = ""; // IP address of the Exchange Server
                                                // or name of the Exchange Server (e.g. "mailServer")

    final static String SUBJECT_TO_SEARCH = "Test";
    final static int SUBSTRING = 1;
    final static int ACTION_DELETE = 3;
    final static int ACTION_REPLY = 4;
    final static int CdoPR_SUBJECT = Integer.parseInt("37001E", 16);
    final static int IGNORECASE = Integer.parseInt("10000", 16);

    public static void main(String[] args) {

        try {

            System.setProperty("JINTEGRA_NATIVE_MODE", "");

            // Authenticate to NT domain via NTLM
            com.linar.jintegra.AuthInfo.setDefault(domain, user, password);

            // Start a MAPI Session
            Session session = new Session(CDOmachine);

            // Logon to the Exchange Server
            session.logon(null, null, new Boolean(false), new Boolean(true),
                          new Integer(0), new Boolean(true),
                          exchangeServer + "\n" + mailbox);

            // Get the inbox
            Folder inbox = new FolderProxy(session.getInbox());

            // for deletion

            Messages m = new MessagesProxy(inbox.getMessages());
            System.out.println("Inbox has " +  m.getCount().toString() + " messages.");
            // ******************

            // Each folder has a Rules collection.
            Rules rules = new Rules(CDOmachine);

            // Create condition for rules.
            PropVal propVal = new PropVal(CDOmachine);
            propVal.setTag(CdoPR_SUBJECT);        // looks in Subject property
            propVal.setValue(SUBJECT_TO_SEARCH);  // looks for this string

            // Create ContentCondition
            ContentCondition condition = new ContentCondition(CDOmachine);
            condition.setOperator(SUBSTRING + IGNORECASE);  // ignores case and other text.

            // Create reply message and store in HiddenMessages collection
            Messages hiddenMessages = new MessagesProxy(inbox.getHiddenMessages());
            Message replyMessage = new MessageProxy(hiddenMessages.add(null, null, null, null));

            // Set reply message properties.
            replyMessage.setText("Please do not send messages to this address");
            replyMessage.update(new Boolean(true), new Boolean(true));

            // Create Reply Rule first.
            Rule replyRule = new Rule(CDOmachine);

            // Set reply action.
            Action replyAction = new Action(CDOmachine);

            Object[] params = new Object[2];
            params[0] = replyMessage.getID();       // ID of template
            params[1] = replyMessage.getFolderID(); // folder ID of template
            replyAction.setArg(params);             // Set the parameters

            // Set other reply rule properties.
            replyRule.setName("Reply Test");
            Actions replyActions = new Actions(replyRule.getActions());
            replyActions.add(null, replyAction);

            // Add reply rule to rules collection.
            rules.add(null, replyRule);

            // Create Delete Rule object.
            Rule deleteRule = new Rule(CDOmachine);

            // Create delete action.
            Action deleteAction = new Action(CDOmachine);

            // Set delete rule properties.
            deleteRule.setName("Delete Test");
            deleteRule.setCondition(condition); // reuse condition
            Actions deleteActions = new Actions(deleteRule.getActions());
            deleteActions.add(null, deleteAction);

            // Add delete rule to the rules collection
            rules.add(null, deleteRule);

            // log off

        } catch (Exception e) {
        } finally {
Related Articles
No Related Articles Available.

Article Attachments

Related External Links
No Related Links Available.
Help us improve this article...
What did you think of this article?


Tell us why you rated the content this way. (optional)
Approved Comments...
No user comments available for this article.
Created on 6/23/2006.
Last Modified on 7/12/2006.
Last Modified by No Author Name Available!.
Article has been viewed 16339 times.
Rated 4 out of 10 based on 30 votes.
Print Article
Email Article