none
Exchange Web Services (EWS) - InvalidCastException RRS feed

  • Question

  • I have a service that reads the new emails in an Office 365 Exchange mailbox, and saves attachments in a directory.

    For most emails it runs well, however I'm getting a lot of the following exceptions lately:

    System.InvalidCastException: Unable to cast object of type 'Microsoft.Exchange.WebServices.Data.ItemAttachment' to type 'Microsoft.Exchange.WebServices.Data.FileAttachment'

    I will post a piece of my code, could anyone please have a look and tell me what I need to change? It's .NET 4.0, Visual Studio 2010, Visual Basic and the latest Exchange Web Services api, 2.2.

    Dim sf As SearchFilter = New SearchFilter.SearchFilterCollection(LogicalOperator.And, New SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, False))
    Dim findResults As FindItemsResults(Of Item) = service.FindItems(WellKnownFolderName.Inbox, sf, New ItemView(128))
    
    If findResults.Count > 0 Then
    	Dim items As ServiceResponseCollection(Of GetItemResponse) =
    		service.BindToItems(findResults.Select(Function(item) item.Id),
    		   New PropertySet(BasePropertySet.FirstClassProperties, EmailMessageSchema.From, EmailMessageSchema.Sender, EmailMessageSchema.ToRecipients, EmailMessageSchema.IsRead))
    
    	For Each itItem As Microsoft.Exchange.WebServices.Data.Item In findResults.Items
    
    		If TypeOf itItem Is EmailMessage Then
    
    			Dim mailItem As EmailMessage = DirectCast(itItem, EmailMessage)
    
    			If itItem.HasAttachments Then
    				itItem.Load()
    				For Each Attachment In itItem.Attachments
    					If TypeOf Attachment Is FileAttachment Then
    						Dim fa As FileAttachment = itItem.Attachments(0)
    						pathAttachment = "c:\temp\" & fa.name 'not actual code, but to make this code readable and to the point
    						Try
    							fa.Load(pathAttachment) 'save file

    Tuesday, January 20, 2015 9:46 AM

Answers

  • Hi Meta,

    Well, that's where your error is coming from. This will throw an error, each time the first attachment is not a FileAttachment but there is a FileAttachment present.

    If there'll always only be one FileAttachment, why not work with the current item?

    Cheers,
    Fred


    There's no place like 127.0.0.1

    • Marked as answer by MetaTurtle Wednesday, January 28, 2015 2:42 PM
    Tuesday, January 27, 2015 11:02 AM

All replies

  • It looks like you've hardcoded to always access the first attachment, when really you want to access the current attachment in the For Each loop. (Unless my VB skills are rusty, which is definitely possible!)

    Dim fa As FileAttachment = itItem.Attachments(0)

    Tuesday, January 20, 2015 2:36 PM
    Moderator
  • That's correct, I always access only the first attachment, that's how this system works by design, emails processed by it should always only have one attachment. But I don't think that is the problem here.
    Tuesday, January 27, 2015 10:58 AM
  • Hi Meta,

    Well, that's where your error is coming from. This will throw an error, each time the first attachment is not a FileAttachment but there is a FileAttachment present.

    If there'll always only be one FileAttachment, why not work with the current item?

    Cheers,
    Fred


    There's no place like 127.0.0.1

    • Marked as answer by MetaTurtle Wednesday, January 28, 2015 2:42 PM
    Tuesday, January 27, 2015 11:02 AM

  • Thanks for your reply Fred and I think you have found the solution. I loop through all attachments and process the first one of the type FileAttachment, however after that I refer to "itItem.Attachments(0)" instead of the current attachment in the loop.
    Wednesday, January 28, 2015 2:42 PM