DecodeHeaders Bug


i think i found two bug when calling
1) I have a couple of mails that have pretty long subjects (and other header values too). These span a couple of lines in the raw mail message. For example:
Subject: =?utf-8?B?QXV0b21hdGlzY2hlIEFudHdvcnQ6QW5zY2hyZWliZW4gYW4gSFIgLSDDnGJl?=
returnValue.Headers["Subject"] "=?utf-8?B?QXV0b21hdGlzY2hlIEFudHdvcnQ6QW5zY2hyZWliZW4gYW4gSFIgLSDDnGJl?= =?utf-8?B?cnByw7xmdW5nIEZ1bmt0aW9uc2Jlc2NocmVpYnVuZyBpbmhhbHRsaWNoIHVu?= =?utf-8?Q?d_SOLL-Profil_auf_Inhalte_bezogen?="
You are using these 2 regex to decode the values in DecodeHeaders:
headers[key] = Regex.Replace(headers[key].ToString(), @"=\?.*?\?Q\?(.*?)\?=", new MatchEvaluator(MyMatchEvaluator), RegexOptions.IgnoreCase | RegexOptions.Multiline);
headers[key] = Regex.Replace(headers[key].ToString(), @"=\?.*?\?B\?(.*?)\?=", new MatchEvaluator(MyMatchEvaluatorBase64), RegexOptions.IgnoreCase | RegexOptions.Multiline);

After this the Subject on the MailMessage only becomes: "d_SOLL-Profil_auf_Inhalte_bezogen"
although this should be: "Anschreiben an Führungskraft - Überprüfung Funktionsbeschreibung inhaltlich und SOLL-Profil auf Inhalte bezogen"
(MAYBE you just have to switch the two statements. But i don't know exactly!)
2) The MatchEvaluator delegate function: MyMatchEvaluatorBase64 seams to be wrong, too:
You are making use of System.Text.Encoding.UTF7 but the actual encoding could be another one (defined in the headers value)
You should parse the encoding in the beginning of the regex, too. (=\?.?\?B\?(.?)\?= becomes =\?(.?)\?B\?(.?)\?= attention the Group Index changes. You should make use of named groups.
System.Text.Encoding enc = System.Text.Encoding.UTF7;
should become:
System.Text.Encoding enc = System.Text.Encoding.GetEncoding(encodingParsedViaRegex);
I hope you can have a look at this soon and fix it.
Thanks, tobi


icnocop wrote Feb 28, 2013 at 10:00 PM

Hi toebens,

Thank you for uploading patch #6687! :)