问题描述
刚刚开始在空白的Visual Studio安装中编写新的ews应用程序。
一切都很好,我用从我的新应用程序发送来的testmail ive对其进行了测试,一切正常。
下一步应该是通过“ findItems”方法从我的收件箱中检索两个测试邮件,但是当我尝试这样做时,我总是会超时。
我不理解,因为发送电子邮件有效,但是检索会超时。 有人可以在这里帮忙吗?
谢谢
这是我现在使用的代码:
{
static void Main(string[] args)
{
ExchangeService service = new ExchangeService();
service.Credentials = new WebCredentials("user","password");
service.Url = new Uri("https://web.mydomain.com/EWS/Exchange.asmx");
if (service != null)
{
ItemView view = new ItemView(10);
view.PropertySet = new PropertySet(BasePropertySet.IdOnly,ItemSchema.Subject);
SearchFilter searchFilter = new SearchFilter.ContainsSubstring(ItemSchema.Subject,"test");
FindItemsResults<Item> findResults = service.FindItems(WellKNownFolderName.InBox,searchFilter,view);
Console.WriteLine(findResults.TotalCount);
}
}
}```
解决方法
对于像您在具有很大项目计数的邮箱文件夹中使用的搜索,超时不会是意外的。我建议您至少在过滤器中加入时间限制,例如
SearchFilter searchFilter = new SearchFilter.ContainsSubstring(ItemSchema.Subject,"test");
SearchFilter DateRestrication = new SearchFilter.IsGreaterThan(ItemSchema.DateTimeReceived,DateTime.Now.AddDays(-7));
SearchFilter sfCol = new SearchFilter.SearchFilterCollection(LogicalOperator.And) { searchFilter,DateRestrication };
FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox,sfCol,view);
或者更好地使用AQS / KQL
FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox,"Subject:test",view);
,
谢谢Glen的回答!
我想知道是否超时,因为目标邮箱的收件箱中只有2封邮件。
我尝试了更改,但收到相同的错误。我认为与服务器的通信通常必须与之相关。也许当我想发送电子邮件时,会使用另一个用于接收邮件的端口?
这就是踪迹所说的:
`
<Trace Tag="EwsRequestHttpHeaders" Tid="1" Time="2020-08-27 07:24:58Z">
POST /EWS/Exchange.asmx HTTP/1.1
Content-Type: text/xml; charset=utf-8
Accept: text/xml
User-Agent: ExchangeServicesClient/15.00.0847.030
Accept-Encoding: gzip,deflate
</Trace>
<Trace Tag="EwsRequest" Tid="1" Time="2020-08-27 07:24:58Z" Version="15.00.0847.030">
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header>
<t:RequestServerVersion Version="Exchange2013_SP1" />
</soap:Header>
<soap:Body>
<m:FindItem Traversal="Shallow">
<m:ItemShape>
<t:BaseShape>IdOnly</t:BaseShape>
<t:AdditionalProperties>
<t:FieldURI FieldURI="item:Subject" />
</t:AdditionalProperties>
</m:ItemShape>
<m:IndexedPageItemView MaxEntriesReturned="10" Offset="0" BasePoint="Beginning" />
<m:ParentFolderIds>
<t:DistinguishedFolderId Id="inbox" />
</m:ParentFolderIds>
<m:QueryString>Subject:test</m:QueryString>
</m:FindItem>
</soap:Body>
</soap:Envelope>
</Trace>