Jürgen Hoffmann
Jürgen Hoffmann

Reputation: 957

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?

Upvotes: 3

Views: 2491

Answers (3)

dePatinkin
dePatinkin

Reputation: 2289

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'
    }
}

Upvotes: 1

Alex
Alex

Reputation: 2609

Browsing EWS sources shows that FileAttachment.FileName property is only populated when the attachment gets saved to local disk with .Load method.

Upvotes: 1

Our Man in Bananas
Our Man in Bananas

Reputation: 5981

I used the emailMessage schema in a similar system and haven't noticed this issue, maybe the below will help:

//creates an object that will represent the desired mailbox
Mailbox mb = new Mailbox(common.strInboxURL);

//creates a folder object that will point to inbox fold
FolderId fid = new FolderId(WellKnownFolderName.Inbox, mb);

//this will bind the mailbox you're looking for using your service instance
Microsoft.Exchange.WebServices.Data.Folder inbox = Microsoft.Exchange.WebServices.Data.Folder.Bind(service, fid);

SearchFilter.SearchFilterCollection searchFilterCollection = new SearchFilter.SearchFilterCollection(LogicalOperator.And);
searchFilterCollection.Add(new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false));


// exclude any silly returned emails
searchFilterCollection.Add(new SearchFilter.Not(new SearchFilter.ContainsSubstring(EmailMessageSchema.Subject, "Undeliverable")));
searchFilterCollection.Add(new SearchFilter.Not(new SearchFilter.ContainsSubstring(EmailMessageSchema.Subject, "Out of Office")));

ItemView view = new ItemView(100);
view.OrderBy.Add(ItemSchema.DateTimeReceived, SortDirection.Ascending);

// This results in a FindItem operation call to EWS.
FindItemsResults<Item> results = service.FindItems(fid, searchFilterCollection, view);

if (results.Count() > 0)
   {

   // set the prioperties we need for the entire result set
   view.PropertySet = new PropertySet(
   BasePropertySet.IdOnly,
   ItemSchema.Subject,
   ItemSchema.DateTimeReceived,
   ItemSchema.DisplayTo, EmailMessageSchema.ToRecipients,
   EmailMessageSchema.From, EmailMessageSchema.IsRead,
   EmailMessageSchema.HasAttachments, ItemSchema.MimeContent,
   EmailMessageSchema.Body, EmailMessageSchema.Sender,
   ItemSchema.Body) { RequestedBodyType = BodyType.Text };

   // load the properties for the entire batch
   service.LoadPropertiesForItems(results, view.PropertySet);

    forech (Microsoft.Exchange.WebServices.Data.Attachment attachment in email.Attachments)
        {

        if (attachment is FileAttachment)
            {
            FileAttachment fileAttachment = attachment as FileAttachment;

Upvotes: 0

Related Questions