Doug Dunn
Doug Dunn

Reputation: 31

How to get file attachments from nested emails using EWS API

I am trying to get all the attachments from a email message that has email messages as attachments. I need to somehow recurse through the attachments to find all the fileAttachments.

For example I have an email that has 2 attachments. First attachment is a file. Second is another email. This second email also has 2 attachments. First attachment is a file. Second is third email. This third email only has one attachment which is a file. So i need to wind up with 3 file Attachments but cant figure out how to loop through this.

Doug

Upvotes: 3

Views: 449

Answers (1)

ryan
ryan

Reputation: 1463

Here is a recursive solution:

Private Function GetFileAttachments(aItem As Item) As IEnumerable(Of FileAttachment)

    Dim result = New List(Of FileAttachment)

    For Each att In aItem.Attachments

        If TypeOf att Is ItemAttachment Then

            Dim itemAttachment = CType(att, ItemAttachment)
            itemAttachment.Load()
            result.AddRange(GetFileAttachments(itemAttachment.Item))

        Else

            result.Add(att)

        End If

    Next

    Return result

End Function

Upvotes: 2

Related Questions