正确理解和使用EIS的FindItems? [英] Understand and use FindItems for EWS correctly?
问题描述
好的,我的大多数应用都在运行,但是我看到使用FindItems的很多变体都有属性和/或限制我感到困惑。 如果有人请查看这一小段代码,我的问题列在下面。 请注意,我在这里设置限制只能获取未读电子邮件,而我正在读取sMailbox变量中设置的邮箱(不是我自己的邮箱)。
Ok, I have most of my app working, but am seeing so many variations of using FindItems with properties and/or restrictions that I'm getting confused. If someone would please review this small bit of code, my questions are listed below. Please note, that I'm setting a restriction here to get Unread email only and I'm reading a mailbox (that is not my own) which is set in the sMailbox variable.
private void GetUnReadEmails(string sMailboxName)
{
// ------------------- Lets now bind to Exchange
// Create the binding
ExchangeServiceBinding esb = new ExchangeServiceBinding();
esb.RequestServerVersionValue = new RequestServerVersion();
Console.WriteLine("Exchange version: " + esb.RequestServerVersionValue.Version);
//esb.RequestServerVersionValue.Version = ExchangeVersionType.Exchange2007_SP1;
ExchangeService service = GetBinding(sMailboxName);
esb.Url = service.Url.ToString();
esb.Credentials = new NetworkCredential(Username, Password, Domain);
Mailbox mb = new Mailbox(sMailboxName);
FolderId fid1 = new FolderId(WellKnownFolderName.Inbox, mb);
// where fid1 was WellKnownFolderName.Inbox
FindFoldersResults findResults = service.FindFolders(fid1, new FolderView(int.MaxValue));
//ItemView iv = new ItemView(100, 0);
//FindItemsResults<Item> findResults = null;
//*** Can I set properties here to get unread only instead of setting restriction below?? ***
//PropertySet psPropSet = new PropertySet(BasePropertySet.IdOnly);
//iv.PropertySet = psPropSet;
//while ((findResults = service.FindItems(fid1, iv)).MoreAvailable == true)
//{
//// PropertySet itItemPropSet = new PropertySet(BasePropertySet.IdOnly) { EmailMessageSchema.Body };
//// service.LoadPropertiesForItems(findResults.Items, itItemPropSet);
//service.LoadPropertiesForItems(findResults, new PropertySet(ItemSchema.Subject, ItemSchema.Body, ItemSchema.DateTimeReceived));
// foreach (Item item in findResults.Items)
// {
// Console.WriteLine(item.Subject);
// Console.WriteLine(item.DateTimeReceived);
// Console.WriteLine(item.Body);
// }
// iv.Offset += findResults.Items.Count;
//}
//----------------------- main code ---------------
FindItemType findItemRequest = new FindItemType();
findItemRequest.Traversal = ItemQueryTraversalType.Shallow;
// Define which item properties are returned in the response
ItemResponseShapeType itemProperties = new ItemResponseShapeType();
itemProperties.BaseShape = DefaultShapeNamesType.AllProperties;
findItemRequest.ItemShape = itemProperties; // Add properties shape to request
// Identify which folders to search to find items
DistinguishedFolderIdType[] folderIDArray = new DistinguishedFolderIdType[1];
folderIDArray[0] = new DistinguishedFolderIdType();
folderIDArray[0].Id = DistinguishedFolderIdNameType.inbox;
// Add folders to request
findItemRequest.ParentFolderIds = folderIDArray;
//--- Get Unread Emails
SetIsReadRestriction(findItemRequest);
GetEmails(esb, findItemRequest);
}
private static ExchangeService GetBinding(string sMailboxName)
{
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
//ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP1);
// --- Set the service credentials
service.Credentials = new NetworkCredential(Username, Password, Domain);
try
{
// --- Set the url.
//service.Url = new Uri(EWS_URL);
service.AutodiscoverUrl(sMailboxName); //, RedirectionUrlValidationCallback);
Console.WriteLine("ServiceURL: " + service.Url.ToString());
}
catch (AutodiscoverRemoteException ex)
{
Console.WriteLine("Exception thrown: " + ex.Error.Message);
}
return service;
}
public void SetIsReadRestriction(FindItemType findItemRequest)
{
//Create unread only restriction --------------------------
RestrictionType restriction = new RestrictionType();
IsEqualToType isEqualTo = new IsEqualToType();
PathToUnindexedFieldType pathToFieldType = new PathToUnindexedFieldType();
pathToFieldType.FieldURI = UnindexedFieldURIType.messageIsRead;
FieldURIOrConstantType constantType = new FieldURIOrConstantType();
ConstantValueType constantValueType = new ConstantValueType();
constantValueType.Value = "0";
constantType.Item = constantValueType;
isEqualTo.Item = pathToFieldType;
isEqualTo.FieldURIOrConstant = constantType;
restriction.Item = isEqualTo;
findItemRequest.Restriction = restriction;
}
public void GetEmails(ExchangeServiceBinding esb, FindItemType findItemRequest)
{
// ------------- Send the request and get the response
FindItemResponseType findItemResponse = esb.FindItem(findItemRequest);
// ------------- read returned
FindItemResponseMessageType folder = (FindItemResponseMessageType)findItemResponse.ResponseMessages.Items[0];
ArrayOfRealItemsType folderContents = (ArrayOfRealItemsType)folder.RootFolder.Item;
ItemType[] items = folderContents.Items;
string sText = "";
if (items != null)
{
foreach (ItemType curItem in items)
{
sText += "Subject: " + (curItem.Subject.Trim()) + " ";
sText += "DisplayTo: " + (curItem.DisplayTo.Trim()) + " ";
sText += "DateTimeReceived: " + (curItem.DateTimeReceived.ToString()) + " ";
sText += "ItemClass: " + (curItem.ItemClass.Trim()) + " ";
sText += "\r\n";
Console.WriteLine("Has attachments: " + curItem.HasAttachments);
Console.WriteLine("Subject: " + curItem.Subject + " Received: " + curItem.DateTimeReceived.ToString());
Console.WriteLine("Body: " + curItem.Body); //COMING BACK BLANK
SetReadStatus(esb, curItem.ItemId);
}
}
else
sText = "No unread emails.";
txtItems.Text = sText;
}
推荐答案
嗨尼尔森,
首先,您使用的是EWS托管API和自动生成的代理。帮自己一个忙,停止使用自动生成的代理。可以删除所有ExchangeServiceBinding,DistinguishedFolderIdType,FindItemType等。
First, you are using both the EWS Managed API and auto-generated proxies. Do yourself a favor and stop using auto-generated proxies. All the ExchangeServiceBinding, DistinguishedFolderIdType, FindItemType and more can be removed.
使用EWS托管API所需的代码非常简单:
The code you need with the EWS Managed API is pretty simple:
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
service.Credentials = new NetworkCredential("user", "password", "domain");
try
{
// --- Set the url.
service.AutodiscoverUrl("user@contoso.com");
Console.WriteLine("ServiceURL: " + service.Url.ToString());
}
catch (AutodiscoverRemoteException ex)
{
Console.WriteLine("Exception thrown: " + ex.Error.Message);
}
ItemView view = new ItemView(100);
view.PropertySet = new PropertySet() { RequestedBodyType = BodyType.Text };
FindItemsResults<Item> findResults;
do
{
findResults = service.FindItems(
WellKnownFolderName.Inbox,
new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, true),
view);
service.LoadPropertiesForItems(findResults, PropertySet.FirstClassProperties);
foreach (Item item in findResults)
{
Console.WriteLine(item.Subject);
Console.WriteLine(item.DateTimeReceived);
Console.WriteLine(item.Body);
}
}
while (findResults.MoreAvailable);
这篇关于正确理解和使用EIS的FindItems?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!