我正在尝试使用以下代码在C#和Exchange Web Services中连接并下载收件箱中的电子邮件附件,但是我遇到了'System.ArgumentOutOfRangeException'错误,我不知道原因。 我已经搜索过答案,但找不到一个,或者我找到的答案都适用于非常旧的EWS版本。
我知道代码的其余部分通常可行,因为我可以访问与电子邮件相关的其他信息,只是无法访问附件。
有人能帮我指出我的错误吗?
提前感谢,
詹姆斯
static void Main(string[] args)
{
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
service.Credentials = new NetworkCredential("MYLOGIN", "MYPASSWORD", "MYDOMAIN");
service.Url = new Uri("https://MYMAILSERVER/EWS/Exchange.asmx");
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, new ItemView(1000));
foreach (Item item in findResults.Items)
{
if (item.HasAttachments && item.Attachments[0] is FileAttachment)
{
FileAttachment fileAttachment = item.Attachments[0] as FileAttachment;
fileAttachment.Load("C:\\temp\\" + fileAttachment.Name);
}
}
}
}
问题已解决,但出现了新的问题
我已经通过将“foreach(Item item in findResults.Items)”更改为“foreach(EmailMessage item in findResults.Items)”来解决问题,但现在我需要找出如何枚举附件 - 有任何想法的人吗?