Use Java Mail package to send and receive Chinese mail encoding, decoding problems and solutions

Source: Internet
Author: User
Tags base64 character set header mail
Coding | solving | problems | Chinese code


Message headers (see rfc822,rfc2047) can contain only us-ascii characters. Any part of the message header that contains a US-ASCII character must be encoded so that it contains only us-ascii characters. Therefore, the use of Java mail to send Chinese mail must be encoded, or others receive your mail can only be garbled a bunch. The solution to the Java Mail package, however, is simple, using the Encode method (such as Encodetext) in the Mimeutility tool that it brings to encode the Chinese information.



Example:



MimeMessage mimemsg = new MimeMessage (mailsession);



Let JavaMail decide what way to encode, the character set of encoded content is the system character Mimemsg.setsubject (Mimeutility.encodetext (Subject));



Encodes with the specified base64 and specifies that the character set of the encoded content is gb2312

Mimemsg.setsubject (Mimeutility.encodetext (Subject, "gb2312", "B"));



Usually there are 2 ways to encode the headers, one is Base64 mode encoding, the other is QP (quoted-printable) encoding, JavaMail choose the encoding method according to the specific situation.



such as the "TXT test" code after the contents are as follows:

=? GBK? Q? Txt=b2=e2=ca=d4



There is a =? GBK? Q?,GBK represents the character set of the content,? The expression is encoded in QP, followed by the encoded Chinese character. So the information encoded with the Mimeutility tool contains information about the encoding.



The body of the message will also be encoded, but it cannot be encoded in the Mimeutility method. The message body encoding information is to be placed in content-transfer-encoding this header parameter, and mimeutility inside the method is to put the encoding of information in the text after the encoded body content. So if you're dealing with text with mimeutility, other mail programs don't normally display your encoded messages because other mail software like Outlook, Foxmail will only decode the message body according to the information inside the content-transfer-encoding.



In fact, the message body part of the encoding JavaMail has automatically done for you, when you send a message, it will decide the encoding, and the encoding of information into the content-transfer-encoding of this message header parameters, and then send. So all you have to do is just put the content of the message into the mail.



There are many ways to encode the body of the message, including Base64 and QP, and some such as 7bit,8bit, because javamail automatically encodes the message body, so I'm not going to do it all.



Example:



Working with message body

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 way to decode message information, which is a method that starts with decode (such as Decodetext)



Example:

String Subject = Mimemsg.getsubject ();

String chsubject = Mimeutility.decodetext (Subject);



For Base64 and QP encoded information, the Decode* method can decode them correctly, but if the specified character set is not correct, then JavaMail will have an error and cannot decode it correctly.



If some mail system will "TXT test" encoded as follows:

=?x-unkown? Q? Txt=b2=e2=ca=d4



The character set specified here is X-unknown, is not a clear character set, JavaMail can not be handled correctly, but "test" the two characters are still encoded in accordance with the GBK character set, so you can manually change the encoding information correctly, and then use the Decode* method to decode.



Example:

if (Str.indexof ("=?x-unknown?") >=0) {

str = Str.replaceall ("X-unknown", "GBK"); Change the encoding information from X-unkown to GBK

try{

str = mimeutility.decodetext (str); And then decode it again.

}catch (Exception E1) {

return str;

}





The decode* method is decoded according to the encoding information contained in the encoding information, so the decode* method is not valid for decoding the message body, because the message body does not contain the encoding information.



As with coding, the decoding of the message body is also done by JavaMail. JavaMail decodes the text of the message according to the information in the content-transfer-encoding.

The client program obtains from the JavaMail the body content character set for iso-8859-1, therefore also wants to convert the character set, for example:



String correctcontent = new String (Content.getbytes ("iso-8859-1"), "gb2312");



Correntcontent is the correct message body.







Related Article

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.