When using javamail API to develop the mail service system, we often encounter many Chinese garbled characters. The following describes how to solve these problems.
1. Send an attachment with a Chinese name to the email server. The name of the attachment received by other email receiving programs is garbled.
Solution:
Base64 encoding is used when setfilename () of the mimebodypart is called. For example:
- Base64encoder ENC =NewBase64encoder ();// This class is located in JRE/lib/RT. Jar
- // FDS is a filedatasource instance
- MBP. setfilename ("=? GBK? B? "+ Enc. encode (FDS. getname (). getbytes () + "? = ");
2. When receiving an email, obtain the email address sent by some email sending programs. The sending Address is garbled.
Solution:
For sending addresses containing Chinese characters, use the mimeutility. decodetex method. For others, convert the address from iso8859_1 to GBK encoding. See the following example.
- Public Static StringGetfrom (Message MSG ){
- StringFrom = "";
- Try{
- If(Msg. getfrom () [0]! =Null)
- From = msg. getfrom () [0]. tostring ();
- If(From. startswith ("=? GB ") | from. startwith (" =? GB ")){
- From = mimeutility. decodetext (from );
- }Else{
- From = stringutil. tochinese (from );
- }
- }Catch(ExceptionE ){
- E. printstacktrace ();
- }
- From = stringutil. replacestr (from, "<", "<");// Replacestr is a string replacement function.
- From = stringutil. replacestr (from, ">", "> ");
- ReturnFrom;
- }
- //// // Tochinese method of stringutil ///////////////// /////////
- Public Static StringTochinese (StringStrvalue ){
- Try{
- If(Strvalue =Null)
- Return Null;
- Else{
- Strvalue =New String(Strvalue. getbytes ("iso8859_1"), "GBK ");
- ReturnStrvalue;
- }
- }Catch(ExceptionE ){
- Return Null;
- }
- }
3. When receiving an email, get the Chinese attachment name of an email, garbled
Solution:
For base64 encoded Chinese characters, base64 decoding is used. Otherwise, the attachment name is converted from iso8859_1 to GBK. For example:
- StringTemp = part. getfilename ();// The part is a part instance.
- If(Temp. startswith ("=? GBK? B? ") & Temp. endswith ("? = "))
- | (Temp. startswith ("=? GBK? B? ") & Temp. endswith ("? = "))){
- Temp = stringutil. getfrombase64 (temp. substring (8, temp. indexof ("? = ")-1 ));
- }Else{
- Temp = stringutil. tochinese (temp );// This method is described above
- }
- ///// // Stringutil's getfrombase64 method /////////
- Public Static StringGetfrombase64 (StringS ){
- If(S =Null)Return Null;
- Base64decoder decoder =NewBase64decoder ();
- Try{
- Byte[] B = decoder. decodebuffer (s );
- Return New String(B );
- }Catch(ExceptionE ){
- Return Null;
- }
- }
Summary of debugging steps for Garbled text:
Basically, there are so many Chinese garbled characters in javamail. If your program encounters Chinese garbled characters, do not be alarmed first, you can use multiple other email sending or receiving programs to verify the link at which the problem occurs. Then, compare the original text with the Garbled text and call the corresponding encoding and decoding method.
Finally, I hope this short article will inspire you and wish you success.