Common Chinese garbled problem in JavaMail and its solution (synthesis)

Source: Internet
Author: User
Tags base64 mail return string
Solve | The problem | chinese | garbled in the use of the JavaMail API to develop the mail service system, we often encounter a lot of Chinese garbled problem, the following is a description of how to solve these problems.

1. Send the attachment to the mail server with Chinese name, the attachment name that is received by other mail receiving program is displayed as garbled

Solution:
Use BASE64 encoding when calling MimeBodyPart's Setfilename (). For example:
    1. Base64encoder enc = new Base64encoder ()//The class is in Jre/lib/rt.jar
    2. FDS as Filedatasource instance
    3. Mbp.setfilename ("=? GBK? B? " +enc.encode ((Fds.getname ()). GetBytes ()) + "? =");


2. When receiving mail, get some email address sent by mail program, send address as garbled

Solution:
For the sending address containing Chinese, use the Mimeutility.decodetex method to convert the address from Iso8859_1 code to GBK encoding for others, see the following example
  1. public static String Getfrom (msg) {
  2. String from= "";
  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 (Exception e) {
  12. E.printstacktrace ();
  13. }
  14. From=stringutil.replacestr (From, "<", "<");//Replacestr for string substitution functions
  15. From=stringutil.replacestr (From, ">", ">");
  16. return from;
  17. }
  18. The Tochinese method of Stringutil//////////////////////////
  19. public static string Tochinese (string strvalue) {
  20. try{
  21. if (strvalue==null)
  22. return null;
  23. else{
  24. strvalue = new String (strvalue.getbytes ("Iso8859_1"), "GBK");
  25. return strvalue;
  26. }
  27. }catch (Exception e) {
  28. return null;
  29. }
  30. }


3. Receive mail, get a message in Chinese attachment name, there are garbled

Solution:
For Base64 encoded in Chinese, the Base64 decoding is used, otherwise the attachment name is iso8859_1 to the GBK encoding conversion, for example:
  1. String temp=part.getfilename ();//part as 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);//The method as previously described
  7. }
  8. The getFromBASE64 method of Stringutil/////////
  9. public static string getFromBASE64 (string s) {
  10. if (s = = null) return null;
  11. Base64decoder decoder = new Base64decoder ();
  12. try {
  13. Byte[] B = Decoder.decodebuffer (s);
  14. return new String (b);
  15. catch (Exception e) {
  16. return null;
  17. }
  18. }



The debugging steps of the garbled problem are summarized as follows:

Basically in JavaMail encountered in the Chinese garbled problem is so much, if your program appears in Chinese garbled, first do not panic, you can use a number of other mail send or receive procedures to verify, see in which link there is a problem, and then carefully control the original and garbled, call the corresponding encoding and decoding method on the line.



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.