andyedinborough / aenetmail

C# POP/IMAP Mail Client
370 stars 153 forks source link

'System.OutOfMemoryException' when trying to getMessage() #94

Closed galringel closed 12 years ago

galringel commented 12 years ago

Hi, I just downloaded the lasted version of AE.NETMail and compiled a dll from it. Next, I wrote a simple code that reads 10 lasts mail from my gmail INBOX.

On the third mail, the function GetMessage() throws an IO exception: "Exception of type 'System.OutOfMemoryException' was thrown."

Stack trace: at System.IO.MemoryStream.set_Capacity(Int32 value) at System.IO.MemoryStream.EnsureCapacity(Int32 value) at System.IO.MemoryStream.WriteByte(Byte value) at AE.Net.Mail.Utilities.ReadLine(Stream stream, Int32& maxLength, Encoding encoding) at AE.Net.Mail.MailMessage.ParseMime(Stream reader, String boundary, Int32 maxLength) at AE.Net.Mail.MailMessage.ParseMime(String body, String boundary) at AE.Net.Mail.MailMessage.ParseMime(Stream reader, String boundary, Int32 maxLength) at AE.Net.Mail.MailMessage.Load(Stream reader, Boolean headersOnly, Int32 maxLength) at AE.Net.Mail.ImapClient.GetMessages(String start, String end, Boolean uid, Boolean headersonly, Boolean setseen) at AE.Net.Mail.ImapClient.GetMessages(Int32 startIndex, Int32 endIndex, Boolean headersonly, Boolean setseen) at AE.Net.Mail.ImapClient.GetMessage(Int32 index, Boolean headersonly, Boolean setseen) at AE.Net.Mail.ImapClient.GetMessage(Int32 index, Boolean headersonly) at Receiver.ImapReceiver.GetMails(String mailBox) in ..\Receiver\ImapReceiver.cs:line 40

How can i solve this issue?

galringel commented 12 years ago

More Information: The IO Exception occurs in: Utilities.cs, function: ReadLine() at line 27: } else mem.WriteByte(b);

function code: internal static string ReadLine(this Stream stream, ref int maxLength, Encoding encoding) { if (stream.CanTimeout) stream.ReadTimeout = 10000;

  var maxLengthSpecified = maxLength > 0;
  byte b;
  using (var mem = new MemoryStream()) {
    while (true) {
      b = (byte)stream.ReadByte();
      if (maxLength > 0) maxLength--;
      if (b == 10 || b == 13) {
        if (mem.Length == 0 && b == 10) continue;
        break;
      } else mem.WriteByte(b);
      if (maxLengthSpecified && maxLength == 0) break;
    }
    return encoding.GetString(mem.ToArray());
  }
}
galringel commented 12 years ago

Bug fix:

On ParseMime replace: while (data != string.Empty && !data.StartsWith(bounderInner)) {

with this: while (data != null && !data.StartsWith(bounderInner))