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

Move header filtering from MimeMessageParser to EmailConverter, thereby enabling access to all parsed headers when using MimeMessageParser directly #487

Closed
erosinnocenti opened this issue Dec 19, 2023 · 4 comments

Comments

@erosinnocenti
Copy link

Hi, first of all, thank you for this great library.
I'm opening this issue to ask if there are any possibility to tweak the ignored headers list used by the MimeMessageParser.
I'm asking this because I'm in a situation where I need to parse an .eml file and retrieve all the "Received" headers.
After a bit of debugging I found the HEADERS_TO_IGNORE list in MimeMessageParser.java which contains all the ignored headers.
Is there any possibility to customize this behaviour?
Thanks in advance!

@bbottema
Copy link
Owner

I finally got around to looking into your use case. The problem with this is, when you build an Email instance, you can do all sorts of stuff with it, not least of all use it to send an email somewhere. In the underlying framework Jakarta/Angus Mail, this is done via headers. These headers are produced by Simple Java Mail to guarantee a correctly configured MimeMessage that behaves accordingly. Not only that, the underlying framework adds more headers that would interfere with this process if they were already included during sending.

Now, if we parse an email file, took these generated headers and kept them, then you have a 'tainted' Email instance. This would be great for simply reading emails, but we can't guarantee a correcting behaviour anymore when sending such emails. So that's why I'm reluctant to allowing in more headers from a source email.

However, having said that, I may be able to offer different solution. I could move the ignoring of the headers out of the MimeMessageParser and further into the Simple Java Mail library where this class is used. Then, you could use the MimeMessageParser directly -which is already public- and then use that instead to parse your email file.

You could then do something like:

MimeMessage mimeMsg = EmailConverter.emlToMimeMessage(yourFile);
ParsedMimeMessageComponents parsed = MimeMessageParser.parseMimeMessage(mimeMsg, /*fetchAttachmentData*/ true or false);
Map<String, Collection<Object>> allHeaders = parsed.getHeaders();

Would this work for you?

@erosinnocenti
Copy link
Author

Hi, thanks for the detailed explanation.
I think it can work for my use case.
At the moment I'm using the MimeMessage.getAllHeaders() to achieve the same result, but if your library could provide the parsed headers would be certainly better.

Also, I noticed that in my first message I have not specified why I need the Received headers.
What I'm trying to do is to retrieve the sent and the received date of an email.
Correct if I'm wrong, but without accessing the Received headers, I can only get the sent date using Email.getSentDate().

@bbottema bbottema changed the title Ignored headers customization in MimeMessageParser Move header filtering from MimeMessageParser to EmailConverter, thereby enabling access to all parsed headers when using MimeMessageParser directly Jan 18, 2024
bbottema added a commit that referenced this issue Jan 18, 2024
…verter. Now, this is now done the build process in EmailConverter, so all headers remain preserved after initial parsing.
@bbottema
Copy link
Owner

That's correct, and by design; the primary focus of this library is to make sending email easy.

I've released this change in 8.6.1. Enjoy!

@bbottema bbottema added this to the 8.6.1 milestone Jan 18, 2024
@erosinnocenti
Copy link
Author

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants