Javamail email garbled

Source: Internet
Author: User

1. garbled email title

The Garbled text of the mail title is well solved. You can use the mimeutility class to convert the email title. The Code is as follows:

 

Mailmessage. setsubject (mimeutility. encodetext (mailinfo. getsubject (), mimeutility. mimecharset ("gb2312"), null ));

The gb2312 can also be changed to UTF-8.

 

 

2. Garbled text in an English Windows email

No Garbled text is displayed on the mail body on my local machine, but Garbled text occurs when I put the program on an English Windows server. It was very strange. Later I found that there would be no garbled characters if I entered Chinese directly into the parameters, but there would be garbled characters if I read the HTML file through the file. The problem is to read the file. The code for reading the file is as follows:

Public void setcontentbypath (string encode, string path) throws unsupportedencodingexception {<br/> stringbuffer filecontent = new stringbuffer (); <br/> try {<br/> file F = new file (PATH); <br/> If (F. isfile () & F. exists () {<br/> inputstreamreader READ = new inputstreamreader (New fileinputstream (F), encode); <br/> bufferedreader reader = new bufferedreader (read ); <br/> string line; <br/> while (line = Reader. Readline ())! = NULL) {<br/> filecontent. append (line ). append (system. getproperty ("line. separator "); <br/>}< br/> Read. close (); <br/>}< br/>}catch (exception e) {<br/> E. printstacktrace (); <br/>}< br/> This. content = filecontent. tostring (); <br/>} 

I tested and found that if reading a file in a Chinese system, you must set encode to gb2312 and iso-8859-1 on an English Windows system. Otherwise, Chinese characters cannot be correctly read. I thought this solved the problem and the email body was garbled! Why? I thought it would also be caused by the UTF-8 encoding set in my email, and changed the UTF-8 to gb2312! UTF-8 is not omnipotent. In summary, gb2312 is used for reading files, and gb2312 is used for sending mails. Do not change to UTF-8.

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.