返回首页

简介
在第1部分中,我讨论了Exchange Server 2007的简短的历史和一个与它的特殊功能的早期预警系统的简要说明。我会尽量保持短的文章,更集中。
在这一部分,我将解释如何阅读就可以寻找不同folderand的消息。
在这里,我们可以有两件事情。阅读所有邮件收件箱文件夹SentItems,草稿等预警系统,为我们提供了分页和排序的设施也可用于搜索这些文件夹中的一些准则
我将在本文中讨论两个。
关于不同的文件夹阅读邮件的几点
在这里,我们使用一个API FindItem预警系统提供的,只返回一个项目一512字节E.返回的对象的所有属性不能被填充,虽然我们可以指定一些我们想要的属性。
关键的早期预警系统的功能这里讨论的是:获取指定文件夹中的项目数获取指定文件夹中的未读项目数读数在指定的文件夹具有以下属性的项目排序(升序或降序)项目:像受某些属性的基础上,从等索引视图:指一个项目的指定号码(传呼)读的项目​​的具体属性搜索文件夹单属性搜索多属性搜索
对于这一切,我们需要像收件箱中的文件夹,SentItems,草稿等FolderID ... ...因此,我们需要调用下面​​这就像一个其他API:

public FolderIdType FindFolderID()

        {

            DistinguishedFolderIdType objSearchRootFolder = new DistinguishedFolderIdType();



            objSearchRootFolder.Id = DistinguishedFolderIdNameType.msgfolderroot;



            FindFolderType requestFindFolder = new FindFolderType();

            requestFindFolder.Traversal = FolderQueryTraversalType.Deep;

  requestFindFolder.ParentFolderIds = new DistinguishedFolderIdType[] { objSearchRootFolder };

            requestFindFolder.FolderShape = new FolderResponseShapeType();

            requestFindFolder.FolderShape.BaseShape = DefaultShapeNamesType.IdOnly;



            //Search filter definition

            requestFindFolder.Restriction = new RestrictionType();



            #region Contains expression



            ContainsExpressionType objContainsExpression = new ContainsExpressionType();

            objContainsExpression.ContainmentMode = ContainmentModeType.FullString;

            objContainsExpression.ContainmentModeSpecified = true;

            objContainsExpression.ContainmentComparison = ContainmentComparisonType.Exact;

            objContainsExpression.ContainmentComparisonSpecified = true;



            PathToUnindexedFieldType objFieldFolderName = new PathToUnindexedFieldType();

            objFieldFolderName.FieldURI = UnindexedFieldURIType.folderDisplayName;

            objContainsExpression.Item = objFieldFolderName;



            objContainsExpression.Constant = new ConstantValueType();

            objContainsExpression.Constant.Value = "Inbox";



            #endregion Contains expression



            requestFindFolder.Restriction.Item = objContainsExpression;



            FindFolderResponseType objFindFolderResponse =

                _esb.FindFolder(requestFindFolder);



            if (objFindFolderResponse.ResponseMessages.Items.Length == 0)

             return null;



            foreach (ResponseMessageType responseMsg in 

                objFindFolderResponse.ResponseMessages.Items)

            {

                if (responseMsg.ResponseClass == ResponseClassType.Success)

                {

                    FindFolderResponseMessageType objFindResponse =

                        responseMsg as FindFolderResponseMessageType;

                    foreach (

                        BaseFolderType objFolderType in objFindResponse.RootFolder.Folders)

                    {

                        return objFolderType.FolderId;

                    }

                }

            }

            return null;

        }
获取一个指定的文件夹中的项目计数
,如果该文件夹不包含任何项目,它会返回-1。{C}获取一个指定的文件夹中的未读项目的计数 读一个指定的文件夹中的项目
public List<messagetype> GetFolderItems()

        {

            

            FindItemType findRequest = new FindItemType();

            findRequest.ItemShape = new ItemResponseShapeType();

            

            ItemResponseShapeType itemProperties = new ItemResponseShapeType();

            // Use the Default shape for the response.            

            itemProperties.BaseShape = DefaultShapeNamesType.Default;



            

  findRequest.ItemShape = itemProperties;





            Set the inbox as the parent search folder in search attachementRequest.

            BaseFolderIdType p_folder = FindFolderID();

            findRequest.ParentFolderIds = new BaseFolderIdType[] { p_folder };

            findRequest.Traversal = ItemQueryTraversalType.Shallow;





            // Perform the inbox search

            FindItemResponseType response = _esb.FindItem(findRequest);

            FindItemResponseMessageType responseMessage =

                response.ResponseMessages.Items[0]

                    as FindItemResponseMessageType;

            if (responseMessage.ResponseCode != ResponseCodeType.NoError)

            {

                throw new Exception(responseMessage.MessageText);

            }

            else

            {

                // Set the next index to use

                ArrayOfRealItemsType items = responseMessage.RootFolder.Item as

                             ArrayOfRealItemsType;

                if (items.Items == null)

                {

                    // no items in the view.                    

                    return null;

                }

                // Create our response list               



                List<messagetype> result = new List<messagetype>(items.Items.Length);

                foreach (MessageType item in items.Items)

                {

                    result.Add(item);

                }

                return result;

            }

        }

进行排序,首先我们需要选择在哪个领域,我要排序,在这里我选择的主题:
FieldOrderType[] fieldsOrder = new FieldOrderType[1];

            fieldsOrder[0] = new FieldOrderType();

PathToUnindexedFieldType pathSortField = new PathToUnindexedFieldType();

pathSortField.FieldURI = UnindexedFieldURIType.itemSubject;

fieldsOrder[0].Order = SortDirectionType.Descending;



findRequest.SortOrder = fieldsOrder;

对于索引视图(即如果我想提出在从服务器的时间只有10个项目),我们可以设置返回的最大项目,可以设置startitem我想。
IndexedPageViewType indexedView = new IndexedPageViewType();

indexedView.BasePoint = IndexBasePointType.Beginning;



indexedView.MaxEntriesReturned = p_intMaxRec;

indexedView.MaxEntriesReturnedSpecified = true;

indexedView.Offset = p_intStartIdx;



findRequest.Item = indexedView;

在这里,如果我想要一些特殊的属性,然后我可以象下面这样做:
 

PathToUnindexedFieldType pathSentDateTime = new PathToUnindexedFieldType();

pathSentDateTime.FieldURI = UnindexedFieldURIType.itemDateTimeSent;



itemProperties.AdditionalProperties = new BasePathToElementType[] { pathSentDateTime;}



findRequest.ItemShape = itemProperties;
搜索文件夹
搜索任何文件夹,我们需要做所有这些属性相同的上述。我们在这里还需要类Res​​trictionType的一个对象:
RestrictionType objRestrictionType = new RestrictionType();

在搜索中,我们有两个选择:搜索类似或sentdatetime等单一属性搜索类似sentdatetime多个属性等。
对于单一的搜索属性,我们需要创建对象的OrType
    OrType orType = new OrType();

其他多个
    AndType andType = new AndType();

和其他类型的对象为ContainsExpressionType
这里ContainsRxpressionType是因为,比方说,如果这个问题是打招呼,我们给值只是"HEL??,那么它会搜索所有的项目,其中主体价值包含"HEL .???/ p >
    ContainsExpressionType expressionType = null;

    List<searchexpressiontype> searchExps = null;

现在为主题的搜索
PathToUnindexedFieldType path.FieldURI = UnindexedFieldURIType.itemSubject;

expressionType = new ContainsExpressionType();

expressionType.Item = path;

expressionType.ContainmentMode = ContainmentModeType.Substring; //seach for substrings

expressionType.ContainmentModeSpecified = true;

expressionType.ContainmentComparison =

    ContainmentComparisonType.IgnoreCaseAndNonSpacingCharacters;

expressionType.ContainmentComparisonSpecified = true;

expressionType.Constant = new ConstantValueType();

expressionType.Constant.Value = Hi? //search for any string containing 

searchExps.Add(expressionType);

同样,我们可以遍历许多properies,因为我们希望:
现在andType
    andType.Items = searchExps.ToArray();

    objRestrictionType.Item = andType;

为OrType
    orType.Items = searchExps.ToArray();

    objRestrictionType.Item = orType;

此限制类型的对象,我们需要在findrequest设置为:
    findRequest.Restriction = p_RestrictionType;

,然后将搜索文件夹的限制。
我将讨论在未来的一部分,从不同的文件夹中读取邮件的详细信息。此外,我将试图掩盖EWS的扩展属性,这是非常有用的,当我们需要为每一个项目的一些自定义属性的主要特点之一。

回答

评论会员:Prashant_Rai 时间:2011/12/27
喜Brij,

感谢您的大文章"编程"(EWS),通过Exchange Server 2007代码项目。

但我的查询是:我想找到收件箱文件夹,不同的用户帐户(即e - mail地址)邮件的计数。例如,我想所有的收件箱中的邮件数"user1@domain.com,从再user2@domain.com"等

请让我知道这个合适的解决方案。

关于
评论会员:sachinoncodeproject 时间:2011/12/27
。一切有关EWS的代理类,但没有找到如何删除一个MailMessage使用EWS的代理

请帮助。

萨钦Pakale
评论会员:swendell 时间:2011/12/27
以下是错误:

错误1'FindFolderID"不宣布。这可能是由于其保障水平无法访问。 C:\用户\斯科特\应用程序数据\本地\临时项目\ WindowsApplication1 \ Form1.vb的29 44 WindowsApplication1

错误2','不'WindowsApplication1.ExchangeServicebinding"的成员。 C:\用户\斯科特\数据\本地\临时项目\ WindowsApplication1 \ Form1.vb的37 20 WindowsApplication1

下面是代码

进口WindowsApplication1.EWS
进口System.Net

公共类Form1

"私有财产作为BaseFolderIdType FindFolderID

私人小组的button1_Click(#System.Object的,BYVAL e由于发件人)把手Button1.Click
Debug.Print("输出:"AMP; GetUnreadFolderItemsCount())

END SUB
 0; 公共功能GetUnreadFolderItemsCount(为整数

"设置的版本,凭证和客户端访问服务器上ExchangeServiceBinding
  ; 昏暗的ESB作为新ExchangeServicebinding()
 0; esb.Credentials =新的NetworkCredential("SCOTT","文","我们")
esb.Url ="htt​​ps://192.168.1.4/ews/exchange.asmx"

& #160; DIM unReadCount为整数= -1
  ; "标识该文件夹的属性返回。
  ; 昏暗的属性作为新FolderResponseShapeType()
DIM ptuft随着新PathToUnindexedFieldType()
ptuft.FieldURI = UnindexedFieldURIType.folderManagedFolderInformation
PathToUnindexedFieldType()= {ptuft PathToUnindexedFieldType(0)}
DIM ptufts properties.AdditionalProperties = ptufts
properties.BaseShape = DefaultShapeNamesType.AllProperties

"中得到的文件夹的请求。
 60; DIM p_folder正如BaseFolderIdType = FindFolderID()
昏暗的要求作为新GetFolderType()
request.FolderIds =新BaseFolderIdType(0){p_folder}
request.FolderShape =属性

"发送请求并获得响应。
DIM响应GetFolderResponseType"
响应= esb.get(请求)
 60; DIM aormt ArrayOfResponseMessagesType = response.ResponseMessages
DIM rmta ResponseMessageType()= aormt.Items
对于每一个rmta
RMT作为ResponseMessageType & #160; 如果rmt.ResponseClass = ResponseClassType [错误]
抛出新的异常(rmt.MessageText)
ELSE
DIM firmt FolderInfoResponseMessageType
 0; firmt = TryCast(RMT,FolderInfoResponseMessageType)
BaseFolderType昏暗的文件夹()= firmt.Folders

 60; 对于每个文件夹中的作为BaseFolderType rfolder
  ; 如果TypeOf运算rfolder FolderType然后
DIM MyFolder中FolderType
  ; MyFolder中= TryCast(rfolder,FolderType)
 0; 如果myFolder.UnreadCountSpecified然后
myFolder.UnreadCount unReadCount =
&# 160; 最终如果
&# 160; 最终如果
& #160; 下一步
完如果
 0; 下一步
返回unReadCount
结束函数



尾类修改,2010年11月16日(星期二)上午8:00
评论会员:Brij 时间:2011/12/27
FindFolderID()是我的自定义函数,我写得到的任何文件夹folderid。这种方法是在我的文章。
您是否面临其他一些问题?
干杯!!
Brij
访问我的博客:

{A}
评论会员:会员2788315 时间:2011/12/27
喜Brij

我用你的例子检索未读文件夹中的项目数,但我不能找到一种方式来获得esb.GetFolder()方法,它始终没有。

GetFolderResponseType响应= _esb.GetFolder < - 这是不活跃

你有什么想法?

的问候,

洪城Soo Kim表示:
评论会员:ramprasadiv 时间:2011/12/27
我已经实现了如何从外汇deatils
但现在我有另外一个问题,如何使用验证机制,除了提供网络凭据的用户名和密码。
请尽快意见。

公共类ExchangeDetailsCollection的属性
{
公共System.Collections.Generic.Listlt; ExchangeDetailsgt;列表=新System.Collections.Generic.Listlt; ExchangeDetailsgt ;();{ BR}
公共ExchangeDetailsCollection()
{
}

}
公共类ExchangeDetails的属性
{
公共字符串的用户名;
公共字符串的名称;
公共字符串电话;
公共字符串的电子邮件;
公共字符串部门;
公共字符串单位电话;
公共字符串公司;
公共字符串移动;
公共字符串的StreetAddress
公共字符串telephoneNumber
公共字符串IPPHONE
公共字符串otherMailbox;

公共ExchangeDetails()
{
}

的[WebMethod]
[返回:ExchangeDetailsCollection]
公共ExchangeDetailsCollection getDetailsFromExchange(字符串的用户名,密码字符串)
{
新ExchangeDetailsCollection ExchangeDetailsCollection集合=

ExchangeServiceBinding ESB =新ExchangeServiceBinding()
esb.Credentials =新的NetworkCredential(用户名,密码,"域");
esb.Url = @"https://myserver/EWS/Exchange.asmx";

/ /表格FindItem要求。
FindItemType findItemRequest =新FindItemType()
findItemRequest.Traversal = ItemQueryTraversalType.Shallow;

/ /定义项目属性在响应中返回

ItemResponseShapeType itemProperties =新ItemResponseShapeType(); & #160;itemProperties.BaseShape = DefaultShapeNamesType.AllProperties;

/ /添加属性形状的要求。
& #160; findItemRequest.ItemShape = itemProperties;

/ /确定哪些文件夹进行搜索,寻找项目。
 0; DistinguishedFolderIdType [] folderIDArray =新DistinguishedFolderIdType [1]
folderIDArray [0] =新DistinguishedFolderIdType()
folderIDArray [0] ID = DistinguishedFolderIdNameType.contacts;

/ /添加文件夹的请求。
findItemRequest.ParentFolderIds = folderIDArray;

尝试
{ / /发送请求,并得到响应。FindItemResponseType findItemResponse esb.FindItem =(findItemRequest)

/ /获取响应消息。
ResponseMessageType [] rmta = findItemResponse.ResponseMessages.Items;

的foreach(ResponseMessageType RMT在rmta)
{

FindItemResponseMessageType responseMessage = findItemResponse.ResponseMessages.Items [0] FindItemResponseMessageType;
ArrayOfRealItemsType realItems = responseMessage.RootFolder.Item ArrayOfRealItemsType;

的foreach(ContactItemType联系在realItems.Items)
{
/ /返回的数据的工作,为每个联系人
ExchangeDetails细节ExchangeDetails()
details.email =(contact.EmailAddresses = NULL放大器;放大器; contact.EmailAddresses.Length GT; 0 contact.EmailAddresses [0]的值:!?"");{ BR}。details.name = contact.DisplayName;
!details.phone =(contact.PhoneNumbers = NULL放大器;放大器; contact.PhoneNumbers.Length GT; 0 contact.PhoneNumbers [0]值:"");{ BR}details.department = contact.Department; / /
details.company = contact.CompanyName;
details.streetAddress =(contact.PhysicalAddresses = NULL放大器;放大器; contact.PhysicalAddresses.Length GT; 0 contact.PhysicalAddresses [0]街:?"");{ BR}
Console.WriteLine("姓名:{0} {1} \ nEmail:{2} \ nPhone:{3}",contact.GivenName,contact.Surname,
(contact.EmailAddresses!= NULL放大器;放大器; contact.EmailAddresses.Length GT; 0 contact.EmailAddresses [0]的值:""),{ BR}。(contact.PhoneNumbers = NULL放大器;放大器; contact.PhoneNumbers.Length GT; 0 contact.PhoneNumbers [0]值:?""));{ BR}
collection.list.Add(详情);}
}
}
赶上(例外五)
{(e.Message); }

返回的集合;}
您好Brij,

我想使用在网站上公布的代码。我理解其工作机制。

不同,我试图让我在Exchange存储的联系人。当我读到计数,它是获取正确的价值。但现在我想每个接触的所有属性。我试图用下面的代码片断,它是不取的细节。

你可以请咨询我就这个问题和纠正我,我犯了一个错误。

公共FolderIdType FindFolderID()
{
ExchangeServiceBinding ESB =新ExchangeServiceBinding()
esb.Credentials =新的NetworkCredential("用户名","PWD","域);
esb.Url = @"https://myserver/EWS/Exchange.asmx";

 60; 新DistinguishedFolderIdType DistinguishedFolderIdType objSearchRootFolder =
objSearchRootFolder.Id = DistinguishedFolderIdNameType.msgfolderroot;

FindFolderType requestFindFolder =新FindFolderType()
requestFindFolder.Traversal = FolderQueryTraversalType.Deep;
requestFindFolder.ParentFolderIds =新DistinguishedFolderIdType objSearchRootFolder [] {}
& #160; 新FolderResponseShapeType requestFindFolder.FolderShape =()
requestFindFolder.FolderShape.BaseShape = DefaultShapeNamesType.IdOnly;

 60; / /搜索过滤器定义
新RestrictionType requestFindFolder.Restriction =()

#地区包含表达式

& #160; 新ContainsExpressionType ContainsExpressionType objContainsExpression =
objContainsExpression.ContainmentMode = ContainmentModeType.FullString;

objContainsExpression.ContainmentModeSpecified =真; objContainsExpression.ContainmentComparison = ContainmentComparisonType.Exact;
  ;
objContainsExpression.ContainmentComparisonSpecified =真;
PathToUnindexedFieldType objFieldFolderName =新PathToUnindexedFieldType()
objFieldFolderName.FieldURI = UnindexedFieldURIType.folderDisplayName;
objContainsExpression.Item = objFieldFolderName;

 60; 新ConstantValueType objContainsExpression.Constant =()
objContainsExpression.Constant.Value ="联系人";

#endregion包含表达式

  ;requestFindFolder.Restriction.Item = objContainsExpression;

FindFolderResponseType objFindFolderResponse =

esb.FindFolder(requestFindFolder);
(objFindFolderResponse.ResponseMessages.Items.Length == 0)
 0;
返回null;
 60; FOREACH(ResponseMessageType
responseMsg  0; objFindFolderResponse.ResponseMessages.Items)
 0; {
(responseMsg.ResponseClass == ResponseClassType.Success)
& #160; {
FindFolderResponseMessageType objFindResponse =
作为FindFolderResponseMessageType responseMsg;
&# 160; FOREACH(
 60; BaseFolderType objFolderType objFindResponse.RootFolder.Folders)
  ; {
 0; 返回objFolderType.FolderId;
& #160; }
  ; }
}

返回null; }
公开名单GetFolderItems()
ExchangeServiceBinding ESB =新ExchangeServiceBinding()
esb.Credentials =新的NetworkCredential("用户名","PWD","域);
esb.Url = @"https://myserver/EWS/Exchange.asmx";

FindItemType findRequest =新FindItemType()
新ItemResponseShapeType findRequest.ItemShape =()
ItemResponseShapeType itemProperties =新ItemResponseShapeType();/ /使用默认形状为响应。
itemProperties.BaseShape = DefaultShapeNamesType.Default;
findRequest.ItemShape = itemProperties;
/ /设置收件箱中,在搜索attachementRequest的母公司搜索文件夹
BaseFolderIdType p_folder = FindFolderID()
findRequest.ParentFolderIds =新BaseFolderIdType p_folder [] {}
findRequest.Traversal = ItemQueryTraversalType.Shallow;
/ /执行收件箱中搜索
esb.FindItem FindItemResponseType响应=(findRequest);
FindItemResponseMessageType responseMessage = response.ResponseMessages.Items [0] FindItemResponseMessageType;
(responseMessage.ResponseCode!= ResponseCodeType.NoError)
{
抛出新的异常(responseMessage.MessageText)
}
ELSE
/ /设置下一个索引,使用
ArrayOfRealItemsType项目= responseMessage.RootFolder.Item为
ArrayOfRealItemsType;
(items.Items == NULL)
{
/ /在视图中没有商品。 返回null;}
/ /创建我们的响应列表
名单结果=新名单(items.Items.Length);
的foreach(items.Items MessageType项目)
{
项目。 result.Add(项目);} 返回结果;}
}

修改,2009年11月10日,日(星期二)0:05
评论会员:LynneBarton 时间:2011/12/27
感谢您的代码。它一直对我非常有帮助。我想寻找更多的不仅仅是收件箱项,但需要在邮件文件夹(不是日历,联系人,任务等)中找到的所有电子邮件,我该怎么办?谢谢!

修改,2009年10月22日,日(星期四)下午12:58
评论会员:hav2play21 时间:2011/12/27
嘿,
这是一个伟大的3部分组成的文章,但我有一个问题。当我经历和运行敛电子邮件的代码,它是不是攫取的一切有关的电子邮件。的意思,我需要的重要信息,如电子邮件的正文和发件人的电子邮件地址,并在ArrayOfRealItemsType领域这些领域都是未来回空。为什么这个有什么想法正在发生的事情,以及如何解决呢?



凯尔 - 琼斯
评论会员:Brij 时间:2011/12/27
您使用的是哪个API获得的电子邮件的细节。
FindItem
FindItem
GetItem
,实际上返回任何你想要的所有属性,然后使用{C16 item.If limites的}大小SOM特定的属性。
让我知道如果这样做是不够的。

干杯!!
Brij:

| Bhavneet83
评论会员:游客 时间:2011/12/27
我的代码工作,但加载和显示的消息是很慢的
Brij
评论会员:游客 时间:2011/12/27
如果加载很慢,然后检查您的服务器的速度。优化您的code.Check如果安装OWA的速度......本文相关内容干杯!!Brij
会员2935228
评论会员:什么是列表 时间:2011/12/27
?Brij
评论会员:游客 时间:2011/12/27
。列表中提供了一个通用NET2.0..你是问别的东西吗?干杯!!Brij
Brij
评论会员:游客 时间:2011/12/27
PathToUnindexedFieldType"pathSortField"也必须映射到fieldsOrder[0]。FieldOrderType[]fieldsOrder=新FieldOrderType[1] fieldsOrder[0]=新FieldOrderType()PathToUnindexedFieldTypepathSortField=新PathToUnindexedFieldType()pathSortField.FieldURI=UnindexedFieldURIType.itemSubject;fieldsOrder[0]订购=SortDirectionType.Descending;**************************************************************{BR}fieldsOrder[0]项目=pathSortField;**************************************************************{BR}findRequest.SortOrder=fieldsOrder;