Exchange Web Services - Processing Messages and Receiving Attachments

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

+7
c # attachment exchangewebservices ews-managed-api
source share
2 answers

I am afraid that I rethought this problem and was able to cure it. Unfortunately, at that time I was too pressed to return here and document the decision. Time passed, and my memory of what I changed disappeared, but as far as I remember, it was a one-line change:

 EmailMessage message = EmailMessage.Bind(service, item.Id, new PropertySet(BasePropertySet.FirstClassProperties, EmailMessageSchema.Attachments)); 

The main difference here is that we specified BasePropertySet.FirstClassProperties as the first propertySet parameter, not the BasePropertySet.IdOnly we originally had.

My original code was taken from an example on the Internet that did exactly what I was trying to achieve, so either the example was not quite right, or I rewrote it incorrectly or misunderstood some aspects of the problem.

+12
source share
 foreach(EmailMessage message in findResults) { message.Load(); 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); } } 
0
source share

All Articles