Home : Searching Messages using CDO
Q120048 - HOWTO: Searching Messages using CDO

Searching Messages using CDO

 

The following Java code searches for e-mails sent to "John" with a subject containing the word "Sales". If search is successful, it prints out the details of the message or messages if there are more than one that met the criteria. This code uses the Java API for CDO which comes with J-Integra for Exchange.
import com.intrinsyc.cdo.*;
import com.linar.jintegra.AuthInfo; 

public class SearchMessages{ 

    // Modify the following variables based on your specific setup
    static final String domain   = "DOMAIN";    //domain where the CDO host machine belongs
                                                //make sure it's all capitalized 

    static final String user     = "username";  //username of Java-Exchange user 

    static final String password = "password";  //password of Java-Exchange user 

    static final String mailbox  = "mailbox";   //mailbox of Java-Exchange user 

    static final String CDOmachine = "000.000.000.000"; // IP address of the CDO host machine
                                                        // or name of the machine (e.g. "user1")
    static final String exchangeServer   = "000.000.000.000"; // IP address of the Exchange Server
                                                              // or name of the Exchange Server (e.g. "mailServer") 

    public static void main (String[] arg ){
        Session session = null;
        String name = "John";
        String title = "Sales"; 

        System.out.println("Finding messages sent to " + name + " with subject - "
                           + title + " . . . \n"); 

        try{
            // Authenticate to NT domain via NTLM
            AuthInfo.setDefault(domain, user, password); 

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

            // Logon to the Exchange Server
            session.logon(null, null, null, null, null, null,
                          exchangeServer + "\n" + mailbox); 

            // Go to the Inbox
            Folder inbox = new FolderProxy(session.getInbox()); 

            // Get the messages collections from inbox
            Messages msgs = new MessagesProxy(inbox.getMessages()); 

            // Create a filter for the messages
            MessageFilter myFilter = new MessageFilterProxy (msgs.getFilter());
            myFilter.setSubject(title);
            int count = ((Integer)msgs.getCount()).intValue(); 

            if( count > 0){
                for(int j=1; j<=count; j++ ){
                    Message message = new MessageProxy(msgs.getItem(new Integer(j)));
                    Recipients recipients = new RecipientsProxy(message.getRecipients()); 

                    // determine if message is sent to John
                    for(int i = 1; i <= ((Integer)recipients.getCount()).intValue(); i++){
                        Recipient recipient = new RecipientProxy(recipients.getItem(new Integer(i)));
                        String fullname = recipient.getName().toString(); 

                        if(fullname.startsWith(name) == true ){
                            printMessageDetails(message);
                            break;
                        }
                    } // end of inner for
                } // end of outer for
            } 

            // logoff from the session
            session.logoff(); 

        } catch(Exception e) {
            e.printStackTrace();
        }
    } 

    public static void printMessageDetails(Message message){
        try{
            System.out.println("Found message! Details as follows: ");
            System.out.println("Subject : " + message.getSubject());
            System.out.println("Sent on : " + message.getTimeSent());
            System.out.print("Sent to : ");
            Recipients recipients = new RecipientsProxy(message.getRecipients()); 

            for(int i = 1; i <= ((Integer)recipients.getCount()).intValue(); i++){
                Recipient recipient = new RecipientProxy(recipients.getItem(new Integer(i)));
                System.out.print(recipient.getName() + ", ");
            } 

            System.out.println("\nMessage : ");
            System.out.println(message.getText());
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
}

Related Articles
No Related Articles Available.

Article Attachments
No Attachments Available.

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

poor 
1
2
3
4
5
6
7
8
9
10

 excellent
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/10/2006.
Last Modified by No Author Name Available!.
Article has been viewed 2974 times.
Rated 3 out of 10 based on 9 votes.
Print Article
Email Article