In the process of writing a simple console application that monitors a specific exchange mailbox, and when emails that meet certain criteria are received, the application will download an XML file attachment and archive the email.
I connected to EWS OK and was able to scroll through any emails, but I'm afraid when it comes to creating an EmailMessage object that I can use to access attachments.
In the example below, the line EmailMessage message = EmailMessage.Bind(...) is executed without errors, but does not return the correct message, so when I access both properties or methods, I get an error: "The reference to the object is not installed in the instance of the object "
I'm new to C #, not to mention EWS, so I'm struggling to find out where to start ...
Code snippet:
public static void FindItems() { try { ItemView view = new ItemView(10); view.OrderBy.Add(ItemSchema.DateTimeReceived, SortDirection.Ascending); view.PropertySet = new PropertySet( BasePropertySet.IdOnly, ItemSchema.Subject, ItemSchema.DateTimeReceived); findResults = service.FindItems( WellKnownFolderName.Inbox, new SearchFilter.SearchFilterCollection( LogicalOperator.Or, new SearchFilter.ContainsSubstring(ItemSchema.Subject, "Sales Enquiry")), view); log2.LogInfo("Total number of items found: " + findResults.TotalCount.ToString()); foreach (Item item in findResults) { log2.LogInfo(item.Id); EmailMessage message = EmailMessage.Bind(service, item.Id, new PropertySet(BasePropertySet.IdOnly, ItemSchema.Attachments)); Console.WriteLine(message.Subject.ToString()); if (message.HasAttachments && message.Attachments[0] is FileAttachment) { FileAttachment fileAttachment = message.Attachments[0] as FileAttachment; fileAttachment.Load("C:\\temp\\" + fileAttachment.Name); fileAttachment.Load(); Console.WriteLine("FileName: " + fileAttachment.FileName); } } } catch (Exception ex) { log2.LogError(ex.InnerException); } }
My code is for accessing attachments directly from MSDN , so I hope it is ... Any ideas
c # attachment exchangewebservices ews-managed-api
Cjm
source share