Skip to content

Make parsing recipients from EML file more lenient #227

Closed

Description

@josdejong

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?

Activity

  1. bbottema commented on Sep 27, 2019

    @bbottema
    Owner

    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.

  2. added this to the 5.4.0 milestone on Sep 27, 2019
  3. added a commit that references this issue on Sep 27, 2019
  4. changed the title [-]javax.mail.internet.AddressException: Illegal semicolon, not in group[/-] [+]Make parsing recipients from EML file more lenient[/+] on Sep 27, 2019
  5. josdejong commented on Sep 27, 2019

    @josdejong
    Author

    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.

  6. bbottema commented on Sep 27, 2019

    @bbottema
    Owner

    I agree, quite surprising.

  7. bbottema commented on Sep 28, 2019

    @bbottema
    Owner

    5.4.0 released.

  8. josdejong commented on Sep 30, 2019

    @josdejong
    Author

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

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions