6

i'm facing a problem decoding a mail with the following subject:

Subject: =?ISO-8859-1?Q?Re: Re: Re: Fwd: (GI ?= =?ISO-8859-1?Q?Support-Id:11729)?=

javamail decodes it as:

=?ISO-8859-1?Q?Re: Re: Re: Fwd: (GI ?= Support-Id:11729)

is this a valid subject at all? or should javamail be able to read this?

regards

3 Answers 3

6

It's malformed. You're not permitted to have whitespace characters in the middle of an RFC 2047 encoded-word, and thus JavaMail stops trying to decode the Subject when it hits a space before it hits the terminal ?=. Most parsers will be flexible about things like this, given that so many messages are malformed in this manner, but JavaMail is a little too strict in this regard. It's not wrong, but it's definitely not "being liberal in what it accepts". This is what the RFC has to say:

IMPORTANT: 'encoded-word's are designed to be recognized as 'atom's by an RFC 822 parser. As a consequence, unencoded white space characters (such as SPACE and HTAB) are FORBIDDEN within an 'encoded-word'. For example, the character sequence

=?iso-8859-1?q?this is some text?=

would be parsed as four 'atom's, rather than as a single 'atom' (by an RFC 822 parser) or 'encoded-word' (by a parser which understands 'encoded-words'). The correct way to encode the string "this is some text" is to encode the SPACE characters as well, e.g.

=?iso-8859-1?q?this=20is=20some=20text?=

You could replace all the spaces in there with the underscore character, but that can get messy because you'd essentially have to write your own parser in order to know when to do that.

You could also try setting the system property mail.mime.decodetext.strict to false, but a cursory look at the JavaMail code looks like that won't help. (Still worth trying, though.)

1
  • @Cancer Lee: writes "I tried the suggestion, it works." So give mail.mime.decodetext.strict a try.
    – dkarp
    Commented Jan 2, 2013 at 17:15
2

Did you call javax.mail.internet.MimeUtility decodeText on the Subject?

3
  • 1
    MimeMessage.getSubject() does that automatically.
    – dkarp
    Commented Jan 18, 2011 at 14:27
  • dkarp, you are right but we have no info on which method Gerhard used.
    – andcoz
    Commented Jan 18, 2011 at 14:45
  • Check his example. The well-formed encoded-word is decoded and the malformed one isn't. He's either called MimeUtility.decodeText() himself or he's called MimeMessage.getSubject(), but in either case another call to decodeText isn't going to help.
    – dkarp
    Commented Jan 18, 2011 at 19:44
0

edit: sorrry this answer is complete crap, there are are two classes in our projects, imlpemeting also MimeUtility and more, doing much crappy stuff. Sorry!!!!!

1
  • 1
    MimeUtiliy in which package? There is no such method in javax.mail Commented Sep 16, 2014 at 15:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.