none
[E2013] [EWSMA] [C#] [windows]: Attachments hit size limit via EWS when Outlook allows them. Encoding bloating size? RRS feed

  • Question

  • I'm trying to send emails via EWS Managed Api and C#. All works well but when I try to upload files that are near the size limit allowed by the exchange server, e.g. 50MB limit per email and I'm uploading 10 attachments totalling 45MB, I get an exception 'The remote server returned an error: (507) Insufficient Storage.' 

    The stack trace is: 

    2016-03-01 12:49:17.904 UTC ERROR localhost [9] EwsSender system@priorysolutions.com Microsoft.Exchange.WebServices.Data.ServiceRequestException: The request failed. The remote server returned an error: (507) Insufficient Storage. ---> System.Net.WebException: The remote server returned an error: (507) Insufficient Storage.
       at System.Net.HttpWebRequest.GetResponse()
       at Microsoft.Exchange.WebServices.Data.EwsHttpWebRequest.Microsoft.Exchange.WebServices.Data.IEwsHttpWebRequest.GetResponse()
       at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.GetEwsHttpWebResponse(IEwsHttpWebRequest request)
       --- End of inner exception stack trace ---
       at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.GetEwsHttpWebResponse(IEwsHttpWebRequest request)
       at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.ValidateAndEmitRequest(IEwsHttpWebRequest& request)
       at Microsoft.Exchange.WebServices.Data.ExchangeService.InternalCreateItems(IEnumerable`1 items, FolderId parentFolderId, Nullable`1 messageDisposition, Nullable`1 sendInvitationsMode, ServiceErrorHandling errorHandling)
       at Microsoft.Exchange.WebServices.Data.Item.InternalCreate(FolderId parentFolderId, Nullable`1 messageDisposition, Nullable`1 sendInvitationsMode)
       at Microsoft.Exchange.WebServices.Data.Item.Save(FolderId parentFolderId)
       at MyLibrary.EwsSender.SendAndSaveCopy(EmailMessage message, IEmailSettings emailSettings)

    When the same attachments are included in an email via outlook we don't hit the size limit. In fact I'm retrieving an incoming email, grabbing the attachments, and some time later including them on a new outgoing email. The text content of the outgoing email is minimal so it's not that which is pushing the email over the size limit. 

    I suspect this is because EWS uses base64 encoding for uploading the attachments which bloats the size limit by 20-30%. Is that correct? Is there some other way to attach files via EWS that will not hit the size limit before outlook would? 

    My code looks like this: 

    EmailMessage message = new EmailMessage(service);
    message.Subject = theSubject;
    message.Body = new MessageBody(theBody);
    message.From = new EmailAddress(theFrom);
    message.To = new EmailAddress(theTo);

    // I have a collection of objects with byte arrays of data to attach
    foreach (var mailAttachment in theAttachments) 
    {
        message.Attachments.AddFileAttachment(mailAttachment.Name, mailAttachment.Data);
    }

    var draftsFolderId = new FolderId(WellKnownFolderName.Drafts, new Mailbox(theMailboxEmailAddress)); var sentItemsFolderId = new FolderId(WellKnownFolderName.SentItems, new Mailbox(theMailboxEmailAddress));                message.Save(draftsFolderId); message.SendAndSaveCopy(sentItemsFolderId);


    and as per the stack trace it's failing on the Save. 

    I don't have the EWS trace to hand (but will try to get it from the customer site), but I believe there was a single 64MB call to EWS with a SaveItem message, so I guess it's trying to save all attachments at once.

    In this case I have multiple files, so would a solution be to call message.Save() before adding the attachments, and then add each attachment one at a time calling message.Update() after each one?

    If that does solve it for multiple files it still wouldn't work if there's a single 45MB file I need to attach. Any ideas for that? 

    thanks, 

    Rory





    • Edited by Rory PS Thursday, March 3, 2016 11:05 AM
    Thursday, March 3, 2016 10:51 AM

All replies

  • >>  I suspect this is because EWS uses base64 encoding for uploading the attachments which bloats the size limit by 20-30%. Is that correct?

    Yes that's correct the MS docs say you should allow aproxy 33%

    >>  Is there some other way to attach files via EWS that will not hit the size limit before outlook would? 

    Using EWS no if you have 10 attachments you should be able to just upload them in separate request to a draft eg save the message first to the drafts folder then use UpdateItem to add each attachment and then send the draft.

    The Insufficient Storage error comes from IIS you can modify the maxRequestLength in WebConfig file to increase the limit to allow for the bloat see https://technet.microsoft.com/en-us/library/hh529949(v=exchg.150).aspx

    Cheers
    Glen

    Friday, March 4, 2016 3:21 AM