Common Chinese garbled characters in javamail and solutions (Comprehensive)

Source: Internet
Author: User
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:

  1. Base64encoder ENC =NewBase64encoder ();// This class is located in JRE/lib/RT. Jar
  2. // FDS is a filedatasource instance
  3. 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.

  1. Public Static StringGetfrom (Message MSG ){
  2. StringFrom = "";
  3. Try{
  4. If(Msg. getfrom () [0]! =Null)
  5. From = msg. getfrom () [0]. tostring ();
  6. If(From. startswith ("=? GB ") | from. startwith (" =? GB ")){
  7. From = mimeutility. decodetext (from );
  8. }Else{
  9. From = stringutil. tochinese (from );
  10. }
  11. }Catch(ExceptionE ){
  12. E. printstacktrace ();
  13. }
  14. From = stringutil. replacestr (from, "<", "<");// Replacestr is a string replacement function.
  15. From = stringutil. replacestr (from, ">", "> ");
  16. ReturnFrom;
  17. }
  18. //// // Tochinese method of stringutil ///////////////// /////////
  19. Public Static StringTochinese (StringStrvalue ){
  20. Try{
  21. If(Strvalue =Null)
  22. Return Null;
  23. Else{
  24. Strvalue =New String(Strvalue. getbytes ("iso8859_1"), "GBK ");
  25. ReturnStrvalue;
  26. }
  27. }Catch(ExceptionE ){
  28. Return Null;
  29. }
  30. }

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:

  1. StringTemp = part. getfilename ();// The part is a part instance.
  2. If(Temp. startswith ("=? GBK? B? ") & Temp. endswith ("? = "))
  3. | (Temp. startswith ("=? GBK? B? ") & Temp. endswith ("? = "))){
  4. Temp = stringutil. getfrombase64 (temp. substring (8, temp. indexof ("? = ")-1 ));
  5. }Else{
  6. Temp = stringutil. tochinese (temp );// This method is described above
  7. }
  8. ///// // Stringutil's getfrombase64 method /////////
  9. Public Static StringGetfrombase64 (StringS ){
  10. If(S =Null)Return Null;
  11. Base64decoder decoder =NewBase64decoder ();
  12. Try{
  13. Byte[] B = decoder. decodebuffer (s );
  14. Return New String(B );
  15. }Catch(ExceptionE ){
  16. Return Null;
  17. }
  18. }

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.
 

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.