如何连接到Exchange服务器并从共享邮箱(非我自己的“myname@mycompany.com”)读取邮件。
到目前为止,这是我的代码:
//Create a service
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
//Autodiscover end point
service.AutodiscoverUrl("someaddress@mycompany.com");
FindFoldersResults folderSearchResults = service.FindFolders(WellKnownFolderName.Inbox, new FolderView(int.MaxValue));
Microsoft.Exchange.WebServices.Data.Folder exchangeMailbox = folderSearchResults.Folders.ToList().Find(
f => f.DisplayName.Equals("NameOfSharedMailboxIwant", StringComparison.CurrentCultureIgnoreCase));
//Set the number of items we can deal with at anyone time.
ItemView itemView = new ItemView(int.MaxValue);
foreach (Microsoft.Exchange.WebServices.Data.Folder folderFromSearchResults in folderSearchResults.Folders)
{
if (folderFromSearchResults.DisplayName.Equals("NameOfSharedMailboxIWant", StringComparison.OrdinalIgnoreCase))
{
Microsoft.Exchange.WebServices.Data.Folder boundFolder =
Microsoft.Exchange.WebServices.Data.Folder.Bind(service, folderFromSearchResults.Id);
SearchFilter unreadSearchFilter =
new SearchFilter.SearchFilterCollection(
LogicalOperator.And, new SearchFilter.IsEqualTo(
EmailMessageSchema.IsRead, false));
//Find the unread messages in the email folder.
FindItemsResults<Item> unreadMessages = boundFolder.FindItems(unreadSearchFilter, itemView);
foreach (EmailMessage message in unreadMessages)
{
message.Load();
Console.WriteLine(message.Subject);
}
}
当我运行此操作时,我会抛出一个异常,说明“SMTP地址没有与之关联的邮箱”:
Microsoft.Exchange.WebServices.Data.Folder exchangeMailbox = folderSearchResults.Folders.ToList().Find(
f => f.DisplayName.Equals("BA", StringComparison.CurrentCultureIgnoreCase));
我错过了什么?我觉得我几乎在那里,这应该根据EWS Managed API 2.0文档工作,但我
答案 0 :(得分:20)
您应该只使用FolderId重载来指定要访问的邮箱。例如,如果您的共享邮箱名为Shared@domain.com,则使用
FolderId SharedMailbox = new FolderId(WellKnownFolderName.Inbox,"Shared@domain.com");
ItemView itemView = new ItemView(1000);
service.FindItems(SharedMailbox,itemView);
也不要使用
ItemView itemView = new ItemView(int.MaxValue);
这不起作用,因为Exchange会限制因限制而返回的最大项目数。始终将findItems和find文件夹的结果分页,请参阅http://blogs.msdn.com/b/exchangedev/archive/2010/03/12/throttling-policies-and-the-ewsfindcountlimit.aspx
干杯 格伦