bbottema/simple-java-mail

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

Closed this issue · 4 comments

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!

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?

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().

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!

Thank you!