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

Source: Internet
Author: User
Tags base64
Encoding
The mail header (see RFC822, RFC2047) can contain only US-ASCII characters. Any part of the mail header that contains non-US-ASCII characters must be encoded so that it contains only US-ASCII characters. Therefore, if you use java mail to send a Chinese email, it must be encoded; otherwise, the emails you receive may only contain a bunch of garbled characters. However, the solution to using the java mail package is simple. You can use the method starting with encode (such as encodeText) in its built-in MimeUtility tool to encode Chinese information.
Example:
MimeMessage mimeMsg = new MimeMessage (mailSession );
// Let javamail decide the encoding method. The character set of the encoding content is the system character set mimeMsg. setSubject (MimeUtility. encodeText (Subject ));
// Use the specified base64 encoding method and specify that the character set of the encoding content is gb2312
MimeMsg. setSubject (MimeUtility. encodeText (Subject, "gb2312", "B "));
Generally, there are two encoding methods for the Mail header, one is base64, the other is QP (quoted-printable), and javamail selects the encoding method based on the actual situation.
For example, the content after "txt test" encoding is as follows:
=? GBK? Q? Txt = B2 = E2 = CA = D4

There is a =? GBK? Q ?, GBK indicates the content character set ,? Q? It is encoded in QP mode, followed by the encoded Chinese characters. Therefore, the information encoded with the MimeUtility tool contains the encoding method information.
The body of the email must be encoded, but it cannot be encoded using the method in MimeUtility. The message body must be encoded in the Content-Transfer-Encoding header parameter. The MimeUtility uses the Encoding method in the encoded body Content. So if you use MimeUtility to process the body, other email programs will not normally display your encoded email, because other email software such as outlook, foxmail only decodes the body of the email based on the Content-Transfer-Encoding information.
In fact, javamail is automatically used to encode the body of the email. When you send the email, it determines the encoding method, put the Encoding information in the Content-Transfer-Encoding header parameter, and then send it again. So all you have to do is simply put the content of the body of the email into the email.
There are many ways to encode the mail body, including base64 and QP, and some other methods, such as 7bit and 8bit. Because javamail is automatically coded for the Mail body, I will not explain it all the way.
Example:
// Process the email body
MimeBodyPart mbp = new MimeBodyPart ();

If (ContentType () = null | ContentType. equals (""))
Mbp. setText (Content );

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.