locked
EWS Managed API : FileAttachment.Content is padded ? RRS feed

  • Question

  • Hi,

    I'm brand new to EWS Managed API .... I've searched the forums but cannot find this case.

    It seems that the FileAttachment.Content property is "right"-padded with 0x00 characters, whenever the file exceeds a certain (very small) size.  I think the code below speaks for itself; there's no special magic occurring.  FYI, our server is Exchange 2007 SP1, and my client has the x64 EWS Managed API installed.

    Any ideas?  Is this why the called it "EWS Managed API 1.0 Beta", or is there something else that I should be doing, so that FileAttachment.Content is the correct size?

    Thanks!
    Ted Taylor.


    ExchangeService exchangeService = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
    exchangeService.Url = new Uri("https://my.server.com/EWS/Exchange.asmx");
    exchangeService.UseDefaultCredentials = true;
    
    // Find all items in the Inbox
    FindItemsResults<Item> findItemsResults = exchangeService.FindItems(WellKnownFolderName.Inbox, new ItemView(int.MaxValue));
    
    foreach (Item item in findItemsResults)
    {
        if (item is EmailMessage)
        {
            EmailMessage emailMessage = (EmailMessage)item;
            emailMessage.Load(PropertySet.FirstClassProperties);
    
            if (emailMessage.HasAttachments)
            {
    
                foreach (Attachment attachment in emailMessage.Attachments)
                {
    
                    if (attachment is FileAttachment)
                    {
                        FileAttachment fileAttachment = (FileAttachment)attachment;
    
                        // This is the correct size
                        fileAttachment.Load(@"C:\TEMP\CORRECT_SIZE.BIN");
                        
                        // This is the incorrect size
                        fileAttachment.Load();
                        System.IO.File.WriteAllBytes(@"C:\TEMP\INCORRECT_SIZE.BIN", fileAttachment.Content);
    
                        // These three values should be the same
                        long length1 = new System.IO.FileInfo(@"C:\TEMP\CORRECT_SIZE.BIN").Length;
                        long length2 = new System.IO.FileInfo(@"C:\TEMP\INCORRECT_SIZE.BIN").Length;
                        long length3 = fileAttachment.Content.LongLength;
    
                        if ((length1 != length2) || (length1 != length3) || (length2 != length3))
                        {
                            Console.WriteLine(@"File Attachment lengths differ.");
                            Console.WriteLine(@"  Length of C:\TEMP\CORRECT_SIZE.BIN   = " + length1.ToString());
                            Console.WriteLine(@"  Length of C:\TEMP\INCORRECT_SIZE.BIN = " + length2.ToString());
                            Console.WriteLine(@"  fileAttachment.Content.LongLength    = " + length3.ToString());
                            Console.WriteLine();
                            Console.WriteLine("Press any key to continue.");
                            Console.ReadKey();
                        }
    
                        System.IO.File.Delete(@"C:\TEMP\CORRECT_SIZE.BIN");
                        System.IO.File.Delete(@"C:\TEMP\INCORRECT_SIZE.BIN");
                    }
                }
            }
        }
    }
    
    Wednesday, June 10, 2009 10:00 PM

Answers

  • Hi Ted,

    Thanks for reporting this issue. We will make sure that it is fixed in the final version of the API. In the meantime, please use FileAttachment.Load(string fileName) to load the contents of your file attachments.
    David Claux | Program Manager - Exchange Web Services
    • Proposed as answer by David Claux - MSFT Wednesday, June 10, 2009 11:01 PM
    • Marked as answer by Ted Taylor Wednesday, June 10, 2009 11:25 PM
    Wednesday, June 10, 2009 11:01 PM