Java emails are not garbled in Chinese

Source: Internet
Author: User
Package COM. onlinebookstore. util. mail; import Java. io. unsupportedencodingexception; import Java. util. properties; import javax. mail. message; import javax. mail. messagingexception; import javax. mail. session; import javax. mail. transport; import javax. mail. internet. internetaddress; import javax. mail. internet. mimemessage; public class mailsenderinfo {// IP address and port of the server on which the email is sent private string mailserverhost; private stri Ng mailserverport = "25"; // The Private string fromaddress address of the mail sender; // The Private string toaddress of the mail recipient; // log on to the mail sending server with the username and password private string username; private string password; // whether authentication is required private Boolean validate = false; // The mail subject private string subject; // The text content of the email is private string content; // the file name of the email attachment is private string [] attachfilenames; /*** // *** get the mail session attribute */public properties getproperties () {properti Es P = new properties (); p. put ("mail. SMTP. host ", this. mailserverhost); p. put ("mail. SMTP. port ", this. mailserverport); p. put ("mail. SMTP. auth ", validate? "True": "false"); Return P;} Public String getmailserverhost () {return mailserverhost;} public void setmailserverhost (string mailserverhost) {This. mailserverhost = mailserverhost;} Public String getmailserverport () {return mailserverport;} public void setmailserverport (string mailserverport) {This. mailserverport = mailserverport;} public Boolean isvalidate () {return validate;} public void setvalidate (Boolean validate) {This. validate = validate;} Public String [] getattachfilenames () {return attachfilenames;} public void setattachfilenames (string [] filenames) {This. attachfilenames = filenames;} Public String getfromaddress () {return fromaddress;} public void setfromaddress (string fromaddress) {This. fromaddress = fromaddress;} Public String GetPassword () {return password;} public void setpassword (string password) {This. password = password;} Public String gettoaddress () {return toaddress;} public void settoaddress (string toaddress) {This. toaddress = toaddress;} Public String GetUserName () {return username;} public void setusername (string username) {This. username = username;} Public String getsubject () {return subject;} public void setsubject (string subject) {This. subject = subject;} Public String getcontent () {return content;} public void setcontent (string textcontent) {This. content = textcontent;} public void sendemail (string tomailadr, string transport, string mailsubject, string htmlcontent) {try {properties props = new properties (); props. put ("mail. SMTP. host "," smtp.qq.com "); props. put ("mail. SMTP. auth "," true "); // authorize this. content = htmlcontent; Session ses = session. getdefaultinstance (props, new myauthenticator (fromaddress, password); // build and mail. host session internetaddress from = new internetaddress (fromaddress); // the sender's email address internetaddress to = new internetaddress (tomailadr); // the recipient's email address mimemessage MSG = new mimemessage (SES ); // create information MSG. setfrom (from); // the address at which the message is sent. addrecipient (message. recipienttype. to, to); // The MSG address to which the information is sent. setsubject ("HTML mail"); // message topic string content = createhtml (getcontent (); // message content MSG. setcontent (content, "text/html; charset = GBK"); Transport TRAN = SES. gettransport (transport); // gets the transmission instance system. out. println ("mailpassword:" + password); Tran. connect (fromaddress, password); // connect to the sender's mailbox transport. send (MSG); Tran. close ();} catch (messagingexception e) {e. printstacktrace () ;}} Public String createhtml (string body) {stringbuffer sb = new stringbuffer (); sb. append ("<HTML>"); sb. append ("<body>"); sb. append (body); sb. append ("</body>"); sb. append ("

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.