Microsoft KB Archive/216721

= How To Filter Messages Using CDO with Visual J++ =

Article ID: 216721

Article Last Modified on 7/1/2004

-

APPLIES TO


 * Microsoft Visual J++ 6.0 Standard Edition
 * Microsoft Collaboration Data Objects 1.21

-



This article was previously published under Q216721



SUMMARY
This article contains a code sample showing how to filter messages with CDO in Visual J++.

The code sample demonstrates the following functionalities:


 * How to set MessageFilter object's properties
 * How to traverse the Messages collection using GetFirst, GetNext, and GetItem methods
 * How to retrieve the attachment files
 * How to save the attachment to the disk
 * How to quit the program using the System object



MORE INFORMATION
Follow these steps to build and run the sample code:


 * 1) Start Visual J++ 6.0 and create a new Console Application project.
 * 2) In the Project-Explorer window, open your Project tree and double-click the Class1.java file created for you.
 * 3) From the Project menu, select Add COM Wrapper, click Microsoft Cdo 1.21 Library, and click OK.
 * 4) Ignore the created code and replace it with the following code. Make sure you make the necessary changes whenever you see the "TO DO" comment.
 * 5) Compile and run the code.

Sample Code
import com.ms.wfc.ui.*; //to use MessageBox import com.ms.com.*; import cdo.*; public class Class1 {  public static void main(String args[]) {     try {   //Creae a session _Session session = (_Session) new Session; //Create a Variant null parameter for later use Variant nullParam = new Variant; nullParam.noParam; //Create a profile and logon Variant profileName = new Variant; profileName.putString(""); //TO DO: put your profile name session.Logon( profileName, nullParam, nullParam,nullParam,                            nullParam, nullParam, nullParam ); //Go to the Inbox Folder inbox = (Folder)session.getInbox.getDispatch; //Get the messages collections inbox Messages msgs =(Messages)inbox.getMessages.getDispatch;

MessageFilter myFilter = (MessageFilter)msgs.getFilter.getDispatch; Variant mySubject = new Variant; mySubject.putString("test"); myFilter.setSubject( mySubject ); Variant msgType = new Variant; msgType.putString("IPM.Note"); MessageBox.show("Message Count = " + msgs.getCount,                        "After Filter"); if(msgs.getCount.toInt==0) {       System.exit(0); }   Message msg = (Message)msgs.GetFirst(msgType).getDispatch; //Or you can use another way to get the first message //Variant index = new Variant. //index.putInt(1); //Message msg = (Message)msgs.getItem(index).getDispatch; //Display some properties of the message MessageBox.show("Sender: " + msg.getSender.toString + "\n" +              "Subject: " + msg.getSubject.toString + "\n" +               "Text: " + msg.getText.toString,                          "The First Message" ); //Get addressEntry object in order to get the email address AddressEntry entry = (AddressEntry)msg.getSender.getDispatch;

//Display the email address of the Sender MessageBox.show("Email Address: " + entry.getAddress.toString,                                                                                        "The First Message"); //Get the attachments collections Attachments attachs = (Attachments)msg.getAttachments.getDispatch;

MessageBox.show("Attach Count = " + attachs.getCount,                         "The First Message"); if(attachs.getCount.toInt==0) {       System.exit(0); }      //Get the first attachment Variant index = new Variant; index.putInt(1); Attachment attach = (Attachment)attachs.getItem(index).getDispatch; MessageBox.show("Attach: DisplayName: "            + attach.getName.toString + "\n"            + "Attach: Source : "            + attach.getSource.toString,                            "The First Message" );

//Save the attachment to a file, //here you may want to check the type of the attachment file Variant fileName = new Variant; fileName.putString("C:\\temp\\testtest.bat"); attach.WriteToFile( fileName );

//Get the second message if(msgs.getCount.toInt<=1) {        System.exit(0); }   msg = (Message)msgs.GetNext.getDispatch; //Or you can use the next line to get the next message //Variant index = new Variant. //index.putInt(2); //Message msg = (Message)msgs.getItem(index).getDispatch; //Display some properties of the message MessageBox.show("Sender: " + msg.getSender.toString + "\n" +              "Subject: " + msg.getSubject.toString + "\n" +               "Text: " + msg.getText.toString,                          "The Second Message" ); //Finally, logoff session.Logoff; //Clean up          session = null; inbox = null; msgs = null; myFilter = null; msg = null; entry = null; attachs = null; attach = null; }    catch( Exception e ) {         e.printStackTrace; MessageBox.show(e.toString, "e.toString"); }    System.out.println( "Project terminates!" ); } }

