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.