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:
- Base64encoder enc = new Base64encoder ()//The class is in Jre/lib/rt.jar
- FDS as Filedatasource instance
- 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
- public static String Getfrom (msg) {
- String from= "";
- 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 (Exception e) {
- E.printstacktrace ();
- }
- From=stringutil.replacestr (From, "<", "<");//Replacestr for string substitution functions
- From=stringutil.replacestr (From, ">", ">");
- return from;
- }
- The Tochinese method of Stringutil//////////////////////////
- public static string Tochinese (string strvalue) {
- try{
- if (strvalue==null)
- return null;
- else{
- strvalue = new String (strvalue.getbytes ("Iso8859_1"), "GBK");
- return strvalue;
- }
- }catch (Exception e) {
- return null;
- }
- }
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:
- String temp=part.getfilename ();//part as 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);//The method as previously described
- }
- The getFromBASE64 method of Stringutil/////////
- public static string getFromBASE64 (string s) {
- if (s = = null) return null;
- Base64decoder decoder = new Base64decoder ();
- try {
- Byte[] B = Decoder.decodebuffer (s);
- return new String (b);
- catch (Exception e) {
- return null;
- }
- }
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.