Common Chinese garbled issues and solutions in Javamail (comprehensive)

xiaoxiao2021-03-06  39

JAGIE Original (Participation: 651, Expert: 2960) Published: 2003-11-12 4:37 PM Updated: 2003-11-12 4:53 PM Version: 1.0 Read: 3515 times

When using JavaMail API to develop mail service systems, we often encounter a lot of Chinese garbled issues. Here, you will introduce how to solve these problems.

1. Send an attachment to the Chinese to the mail server, and the attachment name received by other mail reception programs is displayed as garbled.

Workaround: Use base64 encoding when calling MIMEBODYPART. E.g:

Base64encoder enc = new base64encoder (); // This class is in jre / lib / rt.jar // fds is a FileDataSource instance mbp.setFilename ("=? GBK? B?" Enc.Encode ((fds.getname () ) .getBytes ()) "? =");

2. When receiving the message, get the email address sent by some mail sender, the send address is displayed as garbled

Solution: For sending addresses containing Chinese, use the MIMEUTILITY.DECODETEX method, convert the address from ISO8859_1 encoding into GBK coding, see the following example

Public static string getFrom (message 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 is a string replacement function from = Stringutil.ReplaceTr (from, ">", ">"); Return from;

/// 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") }}}}}} Catch (Exception E) {return null;}}

3. When receiving the mail, get a Chinese attachment name for an email, there is garbled

Solution: For Chinese encoded with base64, use base64 decoding, otherwise ISO8859_1 to GBK encoding conversion will be performed on the attachment name, for example:

String Temp = part.getFileName (); // part is 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 as follows} / Stringutil's getFromBase64 method /

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 step of garbled problem summary:

Basically in the Chinese garbled problem encountered in Javamail, if your program has a Chinese garbled, do not panic, you can use multiple other mail to send or receive programs to verify, see which link has problems , Then carefully control the original text and garbled, call the corresponding encoding decoding method.

Finally, I hope this short article can be inspired by you, I wish you success.

转载请注明原文地址:https://www.9cbs.com/read-77216.html

New Post(0)