我正在使用SearchFilter集合来限制使用EWS请求Exchange 2010邮箱返回的电子邮件。
我连接到服务并打开了邮箱,没有问题。
问题在于我的searchFilter被忽略了,所有电子邮件都被EWS请求返回。
以下是我的代码:
static void Main(string[] args)
{
ServicePointManager.ServerCertificateValidationCallback = CertificateValidationCallBack;
//creates an object that will represent the desired mailbox
Mailbox mb = new Mailbox(@"bbtest@bocuk.local");
// Find all items where the body contains "move reports".
//string qstring = "Body:\"move reports\"";
// Identify the item properties to return.
//view.PropertySet = new PropertySet(BasePropertySet.IdOnly,
//ItemSchema.Subject);
//creates a folder object that will point to inbox fold
FolderId fid = new FolderId(WellKnownFolderName.Inbox, mb);
//this will bind the mailbox you're looking for using your service instance
Folder inbox = Folder.Bind(service, fid);
List<SearchFilter> searchFilterCollection = new List<SearchFilter>();
searchFilterCollection.Add(new SearchFilter.SearchFilterCollection(LogicalOperator.And, new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false)));
searchFilterCollection.Add(new SearchFilter.SearchFilterCollection(LogicalOperator.And, new SearchFilter.IsEqualTo(EmailMessageSchema.HasAttachments, true)));
searchFilterCollection.Add(new SearchFilter.Not(new SearchFilter.ContainsSubstring(ItemSchema.Subject, "FATS")));
searchFilterCollection.Add(new SearchFilter.Not(new SearchFilter.ContainsSubstring(ItemSchema.Subject, "Assignment")));
searchFilterCollection.Add(new SearchFilter.Not(new SearchFilter.ContainsSubstring(ItemSchema.Subject, "Sandbox: Assignment")));
SearchFilter searchFilter = new SearchFilter.SearchFilterCollection(LogicalOperator.Or, searchFilterCollection.ToArray());
ItemView view = new ItemView(100);
string sAttachmentPath = "C:\\Dev\\EWSHelloWorld\\attachments\\";
// Find the first email message in the Inbox that has attachments. This results in a FindItem operation call to EWS.
FindItemsResults<Item> results = service.FindItems(WellKnownFolderName.Inbox, searchFilter, view);
foreach (EmailMessage email in results)
// looping through all the emails
{
System.Diagnostics.Debug.Write("Found attachemnt on msg with subject: " + email.Subject);
.... code removed for brevity!
根据我的理解,searchFilter 只应返回未读带有附件的电子邮件,而且主题不应为 FATS 或 Sandbox: Assignment。
但是这并没有起作用,对 EWS 的请求返回了所有的电子邮件。
请问我做错了什么?
results.items[0].Id
,它会给你所需的 GUID。之后,你可以将其绑定到 ItemId 上,并设置 isRead 属性。 - Bob Bunn