Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javax.mail - problem decoding subject

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

like image 332
Gerhard Presser Avatar asked Jan 18 '11 14:01

Gerhard Presser


2 Answers

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

like image 135
dkarp Avatar answered Nov 12 '22 17:11

dkarp


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

like image 24
andcoz Avatar answered Nov 12 '22 18:11

andcoz