我试图使用下面的代码连接并下载一个附件,使用C#和Exchange从收件箱中的电子邮件中连接和下载附件,但是我得到了一个“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(findResults.Items中的项目)”更改为“foreach (EmailMessage item in findResults.Items)”来解决这个问题,但是现在我需要了解如何通过附件枚举--有人有什么想法吗?
发布于 2012-04-18 20:55:25
检查你的侧写。如果您在轻量级模式下运行,则不会通过邮件下载附件。
添加以下一行
item.Load() // loads the entire message with attachment发布于 2013-10-17 12:13:43
你新问题的答案是
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
//service.Credentials = new NetworkCredential( "{Active Directory ID}", "{Password}", "{Domain Name}" );
service.AutodiscoverUrl("firstname.lastname@MyCompany.com");
FindItemsResults<Item> findResults = service.FindItems(
WellKnownFolderName.Inbox,
new ItemView(10));
foreach (Item item in findResults.Items)
{
Console.WriteLine(item.Subject);
item.Load();
if(item.HasAttachments)
{
foreach (var i in item.Attachments)
{
FileAttachment fileAttachment = i as FileAttachment;
fileAttachment.Load();
Console.WriteLine("FileName: " + fileAttachment.Name);
}
}
}发布于 2011-05-23 21:00:36
除非我遗漏了一些显而易见的东西,否则您所需要做的就是通过item.Attachments进行枚举。
单击这里并向下滚动到您看到Example标题的位置。
https://stackoverflow.com/questions/5991301
复制相似问题