使用Exchange Web服务从Exchange下载附件

我试图使用以下代码使用C#和Exchange Web服务从收件箱中的电子邮件连接和下载附件,但我收到’System.ArgumentOutOfRangeException’错误,我不明白为什么。 我已经google了一个答案,但我找不到一个或我找到的答案是非常旧版本的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 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(findResults.Items中的EmailMessage项)’来排序问题,但现在我需要找出如何通过附件进行枚举 – 任何人的想法?

检查你的个人资料 如果您在轻型模式下运行,则不会下载带有消息的附件。

添加以下行

 item.Load() // loads the entire message with attachment 

你的新问题的答案是

  ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1); //service.Credentials = new NetworkCredential( "{Active Directory ID}", "{Password}", "{Domain Name}" ); service.AutodiscoverUrl("firstname.lastname@MyCompany.com"); FindItemsResults 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); } } } 

除非我遗漏了一些显而易见的东西,否则你需要做的就是通过item.Attachments枚举。

单击此处并向下滚动到您看到Example标题的位置。

从指定数量的电子邮件下载所有附件的解决方案:

  ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013); service.Credentials = new NetworkCredential("login", "password"); service.Url = new Uri("https://mail.Yourservername.com/EWS/Exchange.asmx"); ServicePointManager.ServerCertificateValidationCallback = delegate { return true; }; FindItemsResults findResults = service.FindItems(WellKnownFolderName.Inbox, new ItemView(10)); if (findResults != null && findResults.Items != null && findResults.Items.Count > 0) foreach (EmailMessage item in findResults) { EmailMessage message = EmailMessage.Bind(service, item.Id, new PropertySet(BasePropertySet.IdOnly, ItemSchema.Attachments, ItemSchema.HasAttachments)); foreach (Attachment attachment in message.Attachment { if (attachment is FileAttachment) { FileAttachment fileAttachment = attachment as FileAttachment; fileAttachment.Load(@"Folder\file.name"); } } } 

不要忘记将正确版本的Exchange Server传递给ExchangeService构造函数。

这是一种可用于下载附件的GetAttachmentsFromEmail方法。

 public static void GetAttachmentsFromEmail(ExchangeService service, ItemId itemId) { // Bind to an existing message item and retrieve the attachments collection. // This method results in an GetItem call to EWS. EmailMessage message = EmailMessage.Bind(service, itemId, new PropertySet(ItemSchema.Attachments)); // Iterate through the attachments collection and load each attachment. foreach (Attachment attachment in message.Attachments) { if (attachment is FileAttachment) { FileAttachment fileAttachment = attachment as FileAttachment; // Load the attachment into a file. // This call results in a GetAttachment call to EWS. fileAttachment.Load("C:\\temp\\" + fileAttachment.Name); Console.WriteLine("File attachment name: " + fileAttachment.Name); } else // Attachment is an item attachment. { ItemAttachment itemAttachment = attachment as ItemAttachment; // Load attachment into memory and write out the subject. // This does not save the file like it does with a file attachment. // This call results in a GetAttachment call to EWS. itemAttachment.Load(); Console.WriteLine("Item attachment name: " + itemAttachment.Name); } } }