尝试使用EWS托管API从Exchange Server 2010读取附件时出现以下错误。
请求失败。无法从传输连接读取数据:远程主机强制关闭现有连接。
请在下面找到我的代码。在fileAttachment.Load
获取错误ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
try
{
service.Url = new Uri(ConfigurationManager.AppSettings["EWS"]);
service.Credentials = new NetworkCredential("XXXX", "XXX", "XXX");
FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, new ItemView(100));
EmailMessage message;
foreach (Item item in findResults.Items)
{
message = EmailMessage.Bind(service, item.Id, new PropertySet(BasePropertySet.IdOnly, ItemSchema.Attachments));
message.Load();
if (message.HasAttachments)
{
foreach (Attachment attachment in message.Attachments)
{
if (attachment is FileAttachment)
{
FileAttachment fileAttachment = attachment as FileAttachment;
fileAttachment.Load(@"C:\Mohan\Mail\" + fileAttachment.Name);
}
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}