jstedfast / MailKit

A cross-platform .NET library for IMAP, POP3, and SMTP.

Home Page:http://www.mimekit.net

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

MimeMessage Load Mail that have big attachments

Dylan-Jinx opened this issue · comments

commented

Describe the bug
I use dotnet 7.0 to build IMAP Server, and then every mail have big attachments, so when user use the IMAP Client to pull mail,the MimeMessage Load mail make the application memory up until Server kill this app.

Platform (please complete the following information):

  • OS: WIndows10
  • .NET Runtime: core 7.0
  • MailKit Version: 4.2.0

the MimeMessage Load mail make the application memory up until Server kill this app.

Are you Dispose()ing the messages? It sounds like you aren't.

That said, if you are writing an IMAP server, you should probably be using the MimeReader class instead of MimeParser or MimeMessage.Load().

If you implement your IMAP server correctly, you should be able to get away with NEVER needing to load the entire message.

What you need to do is subclass MimeReader and override the On*Begin/End() methods that you are interested in (which is probably most or all of them for your scenario). Use those overrides to cache information about the message headers, MIME headers (for each part), the structure of the message, the stream offsets for the body parts, etc.

Then, if/when the client requests the full message or a body part, you can look up the stream offsets and just stream the substream of the content the client asked for.

commented

@jstedfast I Use MimeKit.ExperimentalMimeParser but it still happens memory surge.and then I try to use MimeMessage.Dispose(). It can drastically reduce memory usage, and I don't know if that's something you're not advocating.After the memory analysis, it was found that the heap size was enlarged because the Dispose method was not executed, which occupied a large amount of memory.So, now I do not have a good solution, I do not know the author you have a good solution.

I Use MimeKit.ExperimentalMimeParser but it still happens memory surge

Well, obviously, because you are loading messages into memory and messages can be quite large.

I try to use MimeMessage.Dispose(). It can drastically reduce memory usage, and I don't know if that's something you're not advocating.

In your use case, you obviously need to call Dispose() on the messages when you are done with them.

So, now I do not have a good solution, I do not know the author you have a good solution.

I already gave you a solution - DO NOT LOAD MESSAGES INTO MEMORY!

Use the MimeReader class by subclassing it and overriding the On*Begin()/End() and OnHeaderRead() methods to get the information you need such as file/stream offsets, header values, etc.

You do not need to use MimeParser or ExperimentalMimeParser to get the information you need. They might be more convenient, but they are obviously not working out for you, so you need to implement a lower-level solution using MimeReader instead.