System.OutOfMemoryException during download emails

I am using your Aspose email library (v. 24.2) in C# (.NET Framework 4.7.2). Lately my clients often get the System.OutOfMemoryException when downloading emails.
I use this line of code:

ews.SaveMessage(uri, messageStream);

And the stacktrace concerning you is as follows :

w System.Text.StringBuilder.ExpandByABlock(Int32 minBlockCharCount)
w System.Text.StringBuilder.Append(Char* value, Int32 valueCount)
w System.Text.StringBuilder.Append(Char[] value, Int32 startIndex, Int32 charCount)
w System.Xml.XmlTextReaderImpl.ParseText()
w System.Xml.XmlTextReaderImpl.ParseElementContent()
w System.Xml.XmlTextReaderImpl.Read()
w System.Xml.XmlTextReader.Read()
w System.Xml.XmlReader.ReadStartElement()
w Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderzLacP1d90K9IhGEUXKiAxffy_M5bLCxwBNTtzQYyeBGy8uc2ctw.Read232_MimeContentType(Boolean isNullable, Boolean checkType)
w Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderzLacP1d90K9IhGEUXKiAxffy_M5bLCxwBNTtzQYyeBGy8uc2ctw.Read349_MessageType(Boolean isNullable, Boolean checkType)
w Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderzLacP1d90K9IhGEUXKiAxffy_M5bLCxwBNTtzQYyeBGy8uc2ctw.Read686_ArrayOfRealItemsType(Boolean isNullable, Boolean checkType)
w Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderzLacP1d90K9IhGEUXKiAxffy_M5bLCxwBNTtzQYyeBGy8uc2ctw.Read803_ItemInfoResponseMessageType(Boolean isNullable, Boolean checkType)
w Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderzLacP1d90K9IhGEUXKiAxffy_M5bLCxwBNTtzQYyeBGy8uc2ctw.Read805_ArrayOfResponseMessagesType(Boolean isNullable, Boolean checkType)
w Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderzLacP1d90K9IhGEUXKiAxffy_M5bLCxwBNTtzQYyeBGy8uc2ctw.Read821_GetItemResponseType(Boolean isNullable, Boolean checkType)
w Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderzLacP1d90K9IhGEUXKiAxffy_M5bLCxwBNTtzQYyeBGy8uc2ctw.Read1113_GetItemResponse()
w Microsoft.Xml.Serialization.GeneratedAssembly.ArrayOfObjectSerializer448.Deserialize(XmlSerializationReader reader)
w System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
w System.Web.Services.Protocols.SoapHttpClientProtocol.ReadResponse(SoapClientMessage message, WebResponse response, Stream responseStream, Boolean asyncCall)
w System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(String methodName, Object[] parameters)
w #=zh9Fqfcq_HoOXkObKc3_xqZJZ5tEDCTrUjh8nD$EvlR1pwt5Hlg==.GetItem(GetItemType GetItem1)
w #=z_a$X0LfE4f9gH1FXDVgfdNhkhIYA8oI$dWPM$eMFHF69VhukFNBfeuc=.#=zmur8qpk12gqQ(IEnumerable1 #=z4xjSyTc=, List1 #=zIiqxrW0=)
w #=z_a$X0LfE4f9gH1FXDVgfdNhkhIYA8oI$dWPM$eMFHF69VhukFNBfeuc=.#=zfWQW7Ok=(IEnumerable1 #=z4xjSyTc=, IEnumerable1 #=zIiqxrW0=)
w #=za1dYN5humbx591Xnfx1ZeZyw88_5xaOB6lOZBacVU43U.FetchMessages(IEnumerable1 #=z4xjSyTc=, IEnumerable1 #=zIiqxrW0=)
w #=za1dYN5humbx591Xnfx1ZeZyw88_5xaOB6lOZBacVU43U.FetchMessage(String #=zxa6tcJo=)
w #=za1dYN5humbx591Xnfx1ZeZyw88_5xaOB6lOZBacVU43U.SaveMessage(String #=zxa6tcJo=, Stream #=z1k1hb5g=)

Do you have any advice for me?

Hello @albertose ,

It would be great if you provide us with the sample message for our investigation.

Thank you.

In anticipation of an example from one of my customers, I realised that the same error also occurs when trying to write to a PST file.
Here are some stacktraces that might be useful.

ERROR WRITING AN ITEM IN PST FILE
MESSAGE ERROR --------------
Generata eccezione di tipo ‘System.OutOfMemoryException’.
ERROR CODE : -2147024882
HexErrorCode : 0x8007000E
STACKTRACE--------------------
in System.Text.StringBuilder…ctor(String value, Int32 startIndex, Int32 length, Int32 capacity)
in #=ziHs4aRJbZbrmsp_kHvteVQnsjeqTKCA22xBEI2lTITwOfagYWA==.#=z6jEwgStJuEuW(Byte #=zM$NEPQc=)
in #=ziHs4aRJbZbrmsp_kHvteVQnsjeqTKCA22xBEI2lTITwOfagYWA==.#=zugW0NPvwQvNI()
in #=ziHs4aRJbZbrmsp_kHvteVQnsjeqTKCA22xBEI2lTITwOfagYWA==.#=zUPLJumZZps0E()
in #=zKSrDcmrjn_TwRQki928TZWU$jP6oW8om2ES3pztM6HjeIztkzw==.#=zoZ_FmRo=()
in #=zWxVLnuabufnTYLqJbRi4Zhc2fs8PcXlDcZfvAhBRzsBYjb7TfjbPjSg=.#=zkiBGxes=()
in #=zTaM93u0yE30RubG13WT_Be0D3coYgDp5bL4iZW0Yqk7y.Read(Byte[] #=zHvIsDOE=, Int32 #=z_HENPlY=, Int32 #=zp4LTATs=)
in #=zQdr56SUnaOlCSt5MW9R1vxw7ovDL9EAJig==.#=zy8uLhcSHo0tT(Stream #=zJLdQ5Os=, Stream #=z49S$Lt8=, Int32 #=z5dDHZQM=)
in #=zsfg4$Xv69mcyOKMmM2I5xUWhOqFTOC2HT0O8VvRJPW2MD8q$Aw==.#=zJGToc48=(Stream #=zyO$0GNCIVG9Z, Stream #=zvozLzSQbMdL2)
in #=zSOczYVwC18W7cWJQMc_mZXJ6Mb1V_yvBpA==.#=zhV5DUyQ35T18(Stream #=z3$8qKj8=, Encoding #=z3o73Les=)
in Aspose.Email.Storage.Pst.FolderInfo.#=zw9Hwg3fBazfP(MapiMessage #=zyjHSMBY=)
in Aspose.Email.Storage.Pst.FolderInfo.AddMessage(MapiMessage message)

ERROR DOWNLOADING AN ITEM
MESSAGE ERROR --------------
Read operation failed. See internal exception.
ERROR CODE : -2146232800
HexErrorCode : 0x80131620
STACKTRACE--------------------
w System.Net.ConnectStream.Read(Byte[] buffer, Int32 offset, Int32 size)
w System.IO.StreamReader.ReadBuffer(Char[] userBuffer, Int32 userOffset, Int32 desiredChars, Boolean& readToUserBuffer)
w System.IO.StreamReader.Read(Char[] buffer, Int32 index, Int32 count)
w System.Xml.XmlTextReaderImpl.ReadData()
w System.Xml.XmlTextReaderImpl.ParseText(Int32& startPos, Int32& endPos, Int32& outOrChars)
w System.Xml.XmlTextReaderImpl.ParseText()
w System.Xml.XmlTextReaderImpl.ParseElementContent()
w System.Xml.XmlTextReaderImpl.Read()
w System.Xml.XmlLoader.LoadNode(Boolean skipOverWhitespace)
w System.Xml.XmlLoader.LoadDocSequence(XmlDocument parentDoc)
w System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean preserveWhitespace)
w System.Xml.XmlDocument.Load(XmlReader reader)
w #=zLacP1d90K9IhGEUXKiAxffy_M5bLCxwBNTtzQYyeBGy8uc2ctw==.GetReaderForMessage(SoapClientMessage #=z$eoQUiQ=, Int32 #=z5dDHZQM=)
w System.Web.Services.Protocols.SoapHttpClientProtocol.ReadResponse(SoapClientMessage message, WebResponse response, Stream responseStream, Boolean asyncCall)
w System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(String methodName, Object[] parameters)
w #=zh9Fqfcq_HoOXkObKc3_xqZJZ5tEDCTrUjh8nD$EvlR1pwt5Hlg==.GetItem(GetItemType GetItem1)
w #=z_a$X0LfE4f9gH1FXDVgfdNhkhIYA8oI$dWPM$eMFHF69VhukFNBfeuc=.#=zmur8qpk12gqQ(IEnumerable1 #=z4xjSyTc=, List1 #=zIiqxrW0=)
w #=z_a$X0LfE4f9gH1FXDVgfdNhkhIYA8oI$dWPM$eMFHF69VhukFNBfeuc=.#=zfWQW7Ok=(IEnumerable1 #=z4xjSyTc=, IEnumerable1 #=zIiqxrW0=)
w #=za1dYN5humbx591Xnfx1ZeZyw88_5xaOB6lOZBacVU43U.FetchMessages(IEnumerable1 #=z4xjSyTc=, IEnumerable1 #=zIiqxrW0=)
w #=za1dYN5humbx591Xnfx1ZeZyw88_5xaOB6lOZBacVU43U.FetchMessage(String #=zxa6tcJo=)
w #=za1dYN5humbx591Xnfx1ZeZyw88_5xaOB6lOZBacVU43U.SaveMessage(String #=zxa6tcJo=, Stream #=z1k1hb5g=)
w Iperius365.OnedriveModule.Model.Exchange365Manager.Download(Int32 nDownloadTask, FileFolderEntity res, Boolean writeError)

MESSAGE ERROR (INNER EXCEPTION 1)--------------
An exception of the type ‘System.OutOfMemoryException’ was reported.
ERROR CODE : -2147024882
HexErrorCode : 0x8007000E
STACKTRACE--------------------
w System.Net.Security.SecureChannel.Decrypt(Byte[] payload, Int32& offset, Int32& count)
w System.Net.Security._SslStream.ProcessFrameBody(Int32 readBytes, Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest)
w System.Net.Security._SslStream.StartFrameBody(Int32 readBytes, Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest)
w System.Net.Security._SslStream.StartFrameHeader(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest)
w System.Net.Security._SslStream.StartReading(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest)
w System.Net.Security._SslStream.ProcessRead(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest)

Hello @albertose,

Please share the problem file so we can reproduce the error.

Thank you.