从自定义文件夹中读取MSExchange电子邮件

时间:2016-03-30 17:24:13

标签: java directory exchangewebservices

有没有办法使用Java从MS-Exchange中的自定义文件夹中读取电子邮件?我能够从收件箱中读取,但我有自定义文件夹名称,其中电子邮件被移动到我希望在这些文件夹中读取代码。

以下是我目前从收件箱中读取的代码:

ExchangeCredentials credentials = new WebCredentials("userName", "passWORD", "domain");
service.setCredentials(credentials);
service.setUrl(new URI("https://outlook.mycompany.com/ews/exchange.asmx"));

ItemView view = new ItemView(200);

FindItemsResults<Item> findResults = service.findItems(WellKnownFolderName.Inbox , view);

相反,我想要这样的东西:

FindItemsResults<Item> findResults = service.findItems("My Custom Folder" , view);

我已尝试过这个,但它没有工作:

FindItemsResults<Item> findResults = service.findItems(FolderId.getFolderIdFromString("My Custom Folder") , view);

2 个答案:

答案 0 :(得分:1)

您需要使用FindFolder操作来查找相关文件夹的ewsId。通常,执行此操作的简单方法是将要访问的文件夹转换为路径,例如,如果文件夹是收件箱的子文件夹,则路径字符串将是\ Inbox \ Subfolder name,那么您应该能够使用以下内容将路径拆分为多个查找以获取您之后的特定文件夹(这是c#但您应该能够轻松转换为java,因为该类大致相同)

    internal static Folder GetFolderFromPath(ExchangeService service,String MailboxName,String FolderPath)
    {
        FolderId folderid = new  FolderId(WellKnownFolderName.MsgFolderRoot,MailboxName);   
        Folder tfTargetFolder = Folder.Bind(service,folderid);
        PropertySet psPropset = new PropertySet(BasePropertySet.FirstClassProperties);
        String[] fldArray = FolderPath.Split('\\'); 
        for (Int32 lint = 1; lint < fldArray.Length; lint++) { 
            FolderView fvFolderView = new FolderView(1);
            fvFolderView.PropertySet = psPropset;
            SearchFilter  SfSearchFilter = new SearchFilter.IsEqualTo(FolderSchema.DisplayName,fldArray[lint]); 
            FindFoldersResults findFolderResults = service.FindFolders(tfTargetFolder.Id,SfSearchFilter,fvFolderView); 
            if (findFolderResults.TotalCount > 0){ 
            foreach(Folder folder in findFolderResults.Folders){ 
                tfTargetFolder = folder;                
                } 
            } 
            else{ 
                tfTargetFolder = null;  
                break;  
            }     
        }
        if (tfTargetFolder != null)
        {
            return tfTargetFolder;
        }
        else
        {
            throw new Exception("Folder Not found");
        }
    }

干杯 格伦

答案 1 :(得分:0)

在java中,下面的代码似乎正在工作。

C11