Folder Access And Manipulation |
|
Make sure you have completed all the configuration requirements before running any of the examples.
Outlook default folders like the Inbox, Outbox, Calendar, Contacts Folder, etc. are accessed as follows:
// TODO: create and logon to a Session // retrieve the inbox folder Integer folderType = new Integer(CdoDefaultFolderTypes.CdoDefaultFolderInbox); Folder inbox = new FolderProxy(session.getDefaultFolder(folderType)); // use the following folder type to access other Outlook folders // CdoDefaultFolderTypes.CdoDefaultFolderCalendar for the Calendar // CdoDefaultFolderTypes.CdoDefaultFolderContacts for the Contacts folder // CdoDefaultFolderTypes.CdoDefaultFolderDeletedItems for the Deleted Items folder // CdoDefaultFolderTypes.CdoDefaultFolderJournal for the Journal folder // CdoDefaultFolderTypes.CdoDefaultFolderNotes for the Notes folder // CdoDefaultFolderTypes.CdoDefaultFolderOutbox for the Outbox // CdoDefaultFolderTypes.CdoDefaultFolderSentItems for the Sent Items folder // CdoDefaultFolderTypes.CdoDefaultFolderTasks for the Tasks folder |
The following code iterates through all the folders in a user's mailbox and displays them in a tree structure. You could also use this code to recursively search for a folder by name.
import com.intrinsyc.cdo.*;
public class IterateMailboxFolders {
static int level = 0;
public static void main(String[] args) {
//TODO: Change the following parameters based on your setup and configuration
String domain = "mydomain";
String username = "jsmith";
String password = "password";
String CDOmachine = "0.0.0.0";
String exchangeServer = "0.0.0.0";
String mailbox = "jsmith";
try {
com.linar.jintegra.AuthInfo.setDefault(domain, username, password);
// create and logon to a Session
Session session = new Session(CDOmachine);
session.logon(null, null, new Boolean(false), new Boolean(true), null,
new Boolean(true), exchangeServer + "\n" + mailbox);
// get the info stores
InfoStores ifss = new InfoStoresProxy(session.getInfoStores());
int count = ((Integer)ifss.getCount()).intValue();
System.out.println("There are " + count + " Info Stores");
for(int i=1; i<=count; i++){
InfoStore ifs = new InfoStoreProxy(ifss.getItem(new Integer(i)));
System.out.println("\t Info Store " + i + ": " + ifs.getName());
}
// get the mailbox root folder
// it is usually the second item in the list of info stores
InfoStore ifs = new InfoStoreProxy(ifss.getItem(new Integer(2)));
Folder root = new FolderProxy(ifs.getRootFolder());
// build a tree based on the mailbox folders structure
listFolders(root);
// log off
session.logoff();
} catch (Exception e) {
e.printStackTrace();
} finally {
com.linar.jintegra.Cleaner.releaseAll();
}
}
public static void listFolders(Folder node){
level = level + 1;
try{
if(node != null){
//tree appearance
for(int ts = 1; ts < level; ts++){
System.out.print("| ");
}
System.out.print("+----");
System.out.println(node.getName());
Folders sfs = new FoldersProxy(node.getFolders());
Integer ct = (Integer)sfs.getCount();
for(int i = 1; i <= ct.intValue(); i++){
listFolders(new FolderProxy(sfs.getItem(new Integer(i))));
}
}
} catch (Exception e) {
e.printStackTrace();
}finally {
level = level - 1;
}
}
} |
J-Integra® for Exchange allows you to access and manipulate public folders on the Exchange Server. Refer to this knowledge base article for details.
The following code creates a folder named "Unread Emails" under the Inbox.
// TODO: create and logon to a Session
// retrieve the inbox folder
Integer folderType = new Integer(CdoDefaultFolderTypes.CdoDefaultFolderInbox);
Folder inbox = new FolderProxy(session.getDefaultFolder(folderType));
// get collection of subfolders under inbox
Folders inboxSubfolders = new FoldersProxy(inbox.getFolders());
// add a new folder named "Unread Emails" in this collection
Folder newFolder = new FolderProxy(inboxSubfolders.add("Unread Emails"));
|