bbottema/simple-java-mail

Make parsing recipients from EML file more lenient

josdejong opened this issue · 5 comments

I'm working on an application that has to read emails. The API of simple-java-mail looks great, I use it (in Kotlin) like:

val email = EmailConverter.emlToEmail(emailEml)

When actually reading an email though, I get the following error:

26-Sep-2019 16:13:19.159 ERROR [http-nio-8080-exec-1] org.apache.juli.logging.DirectJDKLog: Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.simplejavamail.converter.internal.mimemessage.MimeMessageParseException: Error getting [To] recipient types] with root cause
javax.mail.internet.AddressException: Illegal semicolon, not in group
	at javax.mail.internet.InternetAddress.parse(InternetAddress.java:921)
	at javax.mail.internet.InternetAddress.parseHeader(InternetAddress.java:658)
	at javax.mail.internet.MimeMessage.getAddressHeader(MimeMessage.java:702)
	at javax.mail.internet.MimeMessage.getRecipients(MimeMessage.java:534)
	at org.simplejavamail.converter.internal.mimemessage.MimeMessageParser.retrieveRecipients(MimeMessageParser.java:393)
	at org.simplejavamail.converter.internal.mimemessage.MimeMessageParser.parseToAddresses(MimeMessageParser.java:374)
	at org.simplejavamail.converter.internal.mimemessage.MimeMessageParser.parseMimeMessage(MimeMessageParser.java:102)
	at org.simplejavamail.converter.EmailConverter.mimeMessageToEmailBuilder(EmailConverter.java:64)
	at org.simplejavamail.converter.EmailConverter.mimeMessageToEmail(EmailConverter.java:55)
	at org.simplejavamail.converter.EmailConverter.emlToEmail(EmailConverter.java:111)

This is because the emails I have to read contain from and to addresses which use semicolons ; as separator (the TO field ends with a semicolon in the following example):

...
X-MS-Exchange-Organization-Network-Message-Id:  ********
X-MS-Exchange-Organization-AuthSource:  ********
X-MS-Exchange-Organization-AuthAs:  Anonymous
TO: fake@recipient.com;
From: "Fake Sender" <fake@sender.com>
CKX-Bounce-Address: ********
...

It looks like javax.mail.internet.InternetAddress expects addresses to be comma separated. Is there a way to configure this?

Yikes, I think that's not RFC-compliant! That or it is a bug in the underlying Jakarta JavaMail (unlikely). The mimemessage tries to parse the header in strict mode which doesn't work. I have found a way around basically by duplicating the internal JavaMail code and forcing strict=false :/

I'd release to Maven Central soon, but OSS Sonatype is currently experiencing network issues.

Thanks Benny! Makes sense if there is a strict mode into play. I'm quite confused, doubting whether there is something odd with the mails I try to parse. I can hardly believe that a matured library like JavaMail would have trouble with it. I can open the mails without problem in various mail applications and with some other libraries to parse eml files.

I agree, quite surprising.

5.4.0 released.

Thanks, I can confirm that I can now parse my emails :)