How to get attachments from email attachments using the EWS API

I am trying to get all attachments from an email message that has email messages as attachments. I need to reinstall somehow via attachments to find all files.

For example, I have an email with two attachments. The first attachment is a file. The second is another email. This second letter also contains 2 attachments. The first attachment is a file. The second is the third email. There is only one attachment in this third email, which is a file. So I need to end up with 3 files, but can't figure out how to do it.

Arc

+3


source to share


1 answer


Here's 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

      

+2


source







All Articles