[Exchange-Online] [EWSMA] [C#] [Windows]: EWS - CreateAttachment problem - The underlying connection was closed: An unexpected error occurred on a send RRS feed

  • Question

  • Hello,


    I have a problem with ExchangeServiceBinding.CreateAttachment(...) method (EWS with Office 365)

    • When I try to add a small attachment (e.g. 5 MB), then the method works fine.
    • When I try to add a big attachment (e.g. 12 MB), then the following exception is thrown:

    The underlying connection was closed: An unexpected error occurred on a send.
       at System.Web.Services.Protocols.WebClientProtocol.GetWebResponse(WebRequest request)
       at System.Web.Services.Protocols.HttpWebClientProtocol.GetWebResponse(WebRequest request)
       at System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(String methodName, Object[] parameters)
       at C2EWS.Proxy.ExchangeServiceBinding.CreateAttachment(CreateAttachmentType CreateAttachment1) in d:\source\EWSProxyClasses.cs:line 1471
       at EWSRequest.Program.UploadAttachment(ExchangeServiceBinding esb, ItemIdType parentItemIdType) in d:\source\EWSRequest\Program.cs:line 90
       at EWSRequest.Program.Main(String[] args) in d:\source\EWSRequest\Program.cs:line 59


    Inner exception:

     Unable to write data to the transport connection: An existing connection was forcibly closed by the remote host.
        at System.Net.Sockets.NetworkStream.MultipleWrite(BufferOffsetSize[] buffers)
        at System.Net.Security._SslStream.StartWriting(SplitWritesState splitWrite, SplitWriteAsyncProtocolRequest asyncRequest)
        at System.Net.Security._SslStream.ProcessWrite(BufferOffsetSize[] buffers, SplitWriteAsyncProtocolRequest asyncRequest)
        at System.Net.TlsStream.MultipleWrite(BufferOffsetSize[] buffers)
        at System.Net.Connection.Write(ScatterGatherBuffers writeBuffer)
        at System.Net.ConnectStream.ResubmitWrite(ConnectStream oldStream, Boolean suppressWrite)


    Inner exception:
      An existing connection was forcibly closed by the remote host
         at System.Net.Sockets.Socket.MultipleSend(BufferOffsetSize[] buffers, SocketFlags socketFlags)
         at System.Net.Sockets.NetworkStream.MultipleWrite(BufferOffsetSize[] buffers)


    The C# code is the following:

    private void UploadAttachment(ExchangeServiceBinding esb, ItemIdType parentItemIdType, string attachmentPath)
        // File Attachment
        FileAttachmentType fileAttach = new FileAttachmentType();
        // Attachment details
        byte[] bytes = System.IO.File.ReadAllBytes(attachmentPath);
        fileAttach.Content = bytes;
        fileAttach.IsInlineSpecified = false;
        fileAttach.LastModifiedTimeSpecified = false;
        fileAttach.Name = System.IO.Path.GetFileName(attachmentPath);
        // Create Attachment
        CreateAttachmentType reqCreateAttach = new CreateAttachmentType();
        // Add parent Id
        reqCreateAttach.ParentItemId = parentItemIdType;
        // Add attachment to the query
        reqCreateAttach.Attachments = new AttachmentType[1];
        reqCreateAttach.Attachments[0] = fileAttach;
        // Upload attachment
        CreateAttachmentResponseType resp = esb.CreateAttachment(reqCreateAttach);
        // Check response errors

    Could you advise a solution, please?



    Friday, March 21, 2014 7:12 AM

All replies

  • Robert,

    It sounds like you may be hitting the message size limit for your mailbox. According to the article Exchange Online Limits the maximum message size for Exchange Server 2013 is 10MB. This size limit includes the message and all attachments. I would suggest looking into using SharePoint online as a way to upload large files, or upgrading Office365 to a version that has a higher message size limit.

    I hope this information helps.

    --- Bob --- Microsoft Exchange SDK

    • Edited by Bob Bunn Tuesday, April 1, 2014 8:20 PM Corrected my signature
    Tuesday, April 1, 2014 8:19 PM
  • Hello,

    The maximum size is rather not the problem in this case. As mentioned in my question above, I have a problem uploading messages with 12MB attachments to Office 365 via EWS. According to the documentation you provided a link to, the message size limit in all plans of Office 365 is 25MB.

    When we try the same request using slow network connection (1Mbit/s) there IS problem  - we are NOT able to upload 12 MB attachments.

    When we try the same request using faster network connection (10Mbit/s) there is NO problem - we are able to upload 12 MB attachments.

    It seems like a kind of timeout - probably from Office365.

    Is there a possibility to upload attachment in chunks?


    Tuesday, April 8, 2014 9:12 AM
  • Hi Robert,

    Please let me know whether you were able to solve this issue.

    I am also facing the same issue while using CreateAttachment(...) method.


    Tuesday, June 10, 2014 1:15 PM
  • Hi everybody,

    I recently have the same error with the method FindItem(). Do you have any solution to solve this problem?



    Wednesday, August 15, 2018 3:43 AM
  • Hello All,

    any news regarding this, we are facing similar stuff in customer's 2010 SP3 server.

    Wednesday, May 1, 2019 11:11 AM