Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MaxMimeDepth=1 doesn't set HtmlBody and TextBody #1006

Closed
firat-plutoflume opened this issue Feb 15, 2024 · 3 comments
Closed

MaxMimeDepth=1 doesn't set HtmlBody and TextBody #1006

firat-plutoflume opened this issue Feb 15, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@firat-plutoflume
Copy link

firat-plutoflume commented Feb 15, 2024

Describe the bug
We are trying to minimize our cpu and memory usage by only extracting first level of attachments/emails under an email. MaxMimeDepth option seems to designed for this, but when I pass new ParserOptions() { MaxMimeDepth = 1 } to MimeMessage.Load() it doesn't set HtmlBody and TextBody fields (although I can see them under BodyParts collection)

I believe the reason is we get MimePart instead of a TextPart here when depth=MaxMimeDepth, then it fails check in one of these places (just guessing):

Note: I'm not 100% sure if this is intentional or a 'bug', if not could you please guide how to extract only first level of attachments and have the body fields?

Platform (please complete the following information):

  • OS: Linux
  • .NET Framework: .Net Standard v2.0
  • MimeKit Version: 4.1.0

To Reproduce
Steps to reproduce the behavior:

  1. Call MimeMessage.Load(new ParserOptions() { MaxMimeDepth = 1 }, stream)
  2. Verify there are body parts with text/html and text/plain types
  3. Verify MimeMessage.TextBody and MimeMessage.HtmlBody are null

Expected behavior
I expect MimeMessage.TextBody and MimeMessage.HtmlBody to be not null when there are body parts with text/html and text/plain types

Code Snippets
If applicable, add code snippets to help explain your problem.

using (var stream = new MemoryStream(mimeBytes))
{
    var res = MimeMessage.Load(new ParserOptions() { MaxMimeDepth = 1 }, stream);
    Console.WriteLine(res.TextBody);
}

Additional context
Add any other context about the problem here.

@jstedfast jstedfast added the bug Something isn't working label Feb 15, 2024
@jstedfast
Copy link
Owner

The expected behavior is really going to depend on the particular message you are parsing.

That said, it looks like you took it upon yourself to debug this issue and find the cause (thanks!).

It definitely looks like that logic could use some improvement.

jstedfast added a commit that referenced this issue Feb 15, 2024
When the MaxMimeDepth is reached by the parser, before this fix, the
parser would only instantiate MimePart (and not Multipart or MessagePart
subclasses) in order to avoid recursing even further.

With this fix, it will instaniate any/all MimePart subclasses as well
(such as TextPart).

Fixes issue #1006
@firat-plutoflume
Copy link
Author

firat-plutoflume commented Mar 19, 2024

Sorry @jstedfast I was away for a while and I'm only able to test latest release(4.4.0), I still have problems getting MimeMessage.BodyParts correctly, it looks like text/plain and text/html body parts are skipped when we set depth to 1.

Here is what I get in debugger with this change:
new

Here is what I was getting before:
old

@jstedfast
Copy link
Owner

jstedfast commented Mar 19, 2024

Set MaxMimeDepth = 2

When MaxMimeDepth = 1, the multipart/alternative is treated like a MimePart instead of a Multipart.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants