coding
The message header (see RFC822, RFC2047) can only contain US-ASCII characters. Any part of the mail header contains non-US-ASCII characters must be encoded to allow it to contain only US-ASCII characters. So use Java Mail to send Chinese email must be encoded, otherwise others receive your email can only be garbled. However, the solution to the Java Mail package is simple, and the Chinese information can be encoded with the method of ENCODE in the MIMEUTILITY tool (such as EncodeText).
example:
MIMEMESSAGE MIMEMSG = New MimeMessage (Mailsession);
/ / Let Javamail decide what to encode, the character set of encoded content is the system character set mimemsg.setSubject (mimeutility.encodetext (subject));
// Use the specified Base64 mode to encode and specify the character set of the encoded content is GB2312
MIMEMSG.SETSUBJECT (MimeUtility.EncodeText (Subject, "GB2312", "B");
There are also two ways to encode the mail head, one is Base64 mode encoding, one is a QP (quoted-printable) code, JavaMail selects the encoding method according to the specific situation.
Such as the "TXT test" encoding is as follows:
=? GBK? Q? Txt = b2 = E2 = ca = D4
There is a =? GBK? Q?, GBK indicates the character set of content ,? q? Indicates that the QP mode is encoded, and the back is followed by the encoded Chinese characters. So the information after encoding the encoded method with the MIMEUTILITY tool.
The body of the message is also to be encoded, but it cannot be encoded in the MIMEUTILITY. The information of the encoding method of the mail body is to place in the content-transfer-encoding this mail header parameter, and the method in MimeUtility is to place the encoded manner in the encoded text content. So if you use mimeutility to process your text, then other mail programs will not display your encoded email, because other mail software such as Outlook, Foxmail will only be made according to the information inside content-transfeer-encoding. decoding.
In fact, the code JavaMail in the text part of the mail has been automatically done. When you send an email, it will determine the encoding method, and put the information of the encoding method into the message header parameters, and then send. So what you have to do is to put the content of the text of the message in the email.
There are more encoding methods for the text of the mail, including Base64 and QP mode, and some such as 7bit, 8bit, etc., because JavaMail is automatically encoded for mail text, so I will not have a lot of Xiang.
example:
// Handling the body of the mail
MIMEBODYPART MBP = new mimebodypart ();
IF (contenttype () == null || Contenttype.equals (""))
Mbp.Settext (Content);
Else
Mbp.setContent (Content, Content);
decoding
The MIMEUTILITY tool in the javamail package also provides a method of decoding email information, which is some ways starting with DECODE (such as decodetext)
example:
String subject = mimemsg.getsubject ();
String chsubject = mimeutility.DecodeText (Subject); After the base64 and QP encoding information, the decode * method can decode them correctly, but if the specified character set is wrong, then JavaMail will have an error, and it does not decode it correctly. .
If some mail system encodes the "TXT test" as follows:
=? x-unkown? q? txt = b2 = E2 = ca = D4
The character set specified here is X-Unknown, and is a non-clear character set. JavaMail cannot be handled correctly, but "test" these two Chinese characters are also encoded by GBK character set, so you can manually encode information Change correct, then decode it with a decode * method.
example:
IF (str.indexof ("=? x-unknown?")> = 0) {
Str = str.replaceAll ("X-UnkNown", "GBK"); // Change the information of the encoding method from X-Unkown to GBK
Try {
Str = mimeutility.decodetext (str); // re-decoding
} catch (exception E1) {
Return Str;
}
Both the decode * method decodes based on the information of the encoding method included in the encoded information, so the decode * method is invalid for the message body, because information does not include the encoding method in the message.
Like the coding, the decoding of the text body is also made by Javamail. JavaMail decodes the body of the message according to the information in Content-Transfer-Encoding. The text content character set from JavaMail is ISO-8859-1, so it is also necessary to convert the character set.
String CorrectContent = New String (Content.getbytes ("ISO-8859-1"), "GB2312");
CorrentContent is the correct message body