JavaMail to eliminate the message garbled

Source: Internet
Author: User
I have been thinking impassability, RFC822 standards have been released for many years, why there are so many inconsistent with the standard of email to appear? One may be the server's problem, and the other is the irresponsible programmer's fault. So I suddenly realized that not only cold-blooded and ruthless bosses and aggressive clients have made programmers physically and psychologically suffer, but the casual or Ching of peers is exacerbating the damage.
I am facing an email in the original issued above the emotion, the customer complained that the message received in the system in the body text display is garbled. This is a typical irregular message: Content-type:text/plain, there is no description of CharSet, and the next text is directly without any encoding of the Chinese text. But Subject is in line with the standard (=?gb2312? b?xxxxxxx?=).
Row line Color of the substandard mail has seen many, the most common is that some header is not encoded, there may be, the body code and subject not encoded, the most annoying is the whole email has no coding information.
Hate to hate, the problem has to be solved, I modified the code, processing logic is as follows:
1. At the beginning of parsing the message, first parse some header that may have encoded information, and record it as Headercharset, and some of the code is as follows:
private static Pattern Encodestringpattern = Pattern.compile ("=? +)? (b| Q)? (. +)? = ", Pattern.case_insensitive | Pattern.dotall);
Private final string[] Chartset_header = new string[] {"Subject", "from", "to", "Cc", "delivered-to"};
..........
enumeration enum = Message.getmatchingheaderlines (Chartset_header);
while (Enum.hasmoreelements ()) {
String header = (string) enum.nextelement ();
Matcher m = Encodestringpattern.matcher (header);
if (M.find ()) {
This.headcharset = M.group (1);
Log.debug ("Guess Mail charset is" + this.headcharset);
Break
}
}
2. Then parse the message body, find out whether to indicate charset information, if specified, record as bodycharset; if not, use Headercharset, if Headercharset is null, Use the default CharSet. It's usually iso-8859-1.
3. Finally processing mail header, if there is no charset information, use Bodycharset, otherwise use the default charset.
The above solution, as long as the body of the message or header in a provision of coding information, then may be able to avoid garbled production, if the thousand killed mail, the body with gb2312 encoding, Subject is not encoded in Japanese, then I can only sigh was defeated. If the entire message is not encoded, unless you are sure that the message is a specified encoding and transcoding, it is only resigned.
Finally, we must call for a sound, please follow the norms!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.