Search code examples
c#exchangewebservices

EWS FileAttachment without FileName


I'm using EWS to recieve mails and import them to our CRM-System. In 99% of the mails everything works fine. But no I have the problem, that some fileattachments dont tell me the filename.

here's an example of my code.

Item item = Item.Bind("id"); //id should be replaced by a leagle id

PropertySet ps = PropertyHelper.GetFullLoadItemPropertySet(m_Item.GetType()); 
//the propertyset is a manually created set with all relevant properties.

item.Load(ps)M

foreach (Attachment att in item.Attachments)
{                                    {
    FileAttachment fa = att as FileAttachment;
    if (fa != null)
    {
        fa.Load();
        if (string.IsNullOrWhiteSpace(fa.FileName))
        {
            System.Diagnostics.Debugger.Break();
        }
    }
}

If I look at the same mail using a little vba-code the attachment has a filename, which is displayed in outlook.

Dim mail As MailItem

Set mail = Application.ActiveExplorer().Selection.Item(1)

debug.Print mail.Attachments.Item(0).FileName

Does anyone have an idea why outlook gets the correct filename, but EWS is telling me that the attachment has no filename?


Solution

  • Not all of the Attachment items you get from EWS are actual FileAttachment items. Add a check for the attachment type.

    foreach (Attachment att in item.Attachments)
    {              
        if (att is FileAttachment fa)
        {
            // Do something with 'fa'
        }
    }