Package COM. util. mail;/*** // The basic information used to send an email * Author by wangfunhttp: // www.5a5w.cn novels 520 */import Java. util. properties; public class mailsenderinfo {// IP address and port of the server sending mail private string mailserverhost; private string mailserverport = "25"; // Private string fromaddress; // The email recipient's address: Private string toaddress; // the username and password used to log on to the email sending server: Private string username; private string password; // whether authentication is required Private Boolean validate = false; // Email Subject private string subject; // The text content of the email private string content; // the file name of the email attachment private string [] attachfilenames; /*** // *** get mail session properties */public properties getproperties () {properties 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 ;}} package COM. util. mail; import Java. util. date; import Java. util. properties; import javax. mail. address; import javax. mail. bodypart; import javax. mail. message; import javax. mail. messagingexception; import javax. mail. multipart; import javax. mail. session; import javax. mail. transport; import javax. mail. internet. internetaddress; import javax. mail. internet. mimebodypart; import javax. mail. internet. mimemessage; import javax. mail. internet. mimemultipart;/***** // *** simple email (email without attachment) sender http://www.bt285.cn BT download */public class simplemailsender {/*** // *** send mail in text format * @ Param mailinfo message for the mail to be sent */Public Boolean sendtextmail (mailsenderinfo mailinfo) {// determine whether identity authentication is required. myauthenticator authenticator = NULL; properties pro = mailinfo. getproperties (); If (mailinfo. isvalidate () {// If identity authentication is required, create a authenticator = new myauthenticator (mailinfo. getUserName (), mailinfo. getPassword ();} // construct a mail sending session sendmailsession = session based on the mail session attribute and password validators. getdefaultinstance (Pro, Authenticator); try {// create an email message mailmessage = new mimemessage (sendmailsession) based on the session; // create the mail sender address from = new internetaddress (mailinfo. getfromaddress (); // sets the sender of the email message mailmessage. setfrom (from); // create the recipient address of the email and set it to address to = new internetaddress (mailinfo. gettoaddress (); mailmessage. setrecipient (message. recipienttype. to, to); // set the subject of the email message mailmessage. setsubject (mailinfo. getsubject (); // set the mail message sending time mailmessage. setsentdate (new date (); // you can specify string mailcontent = mailinfo. getcontent (); mailmessage. settext (mailcontent); // send the email transport. send (mailmessage); Return true;} catch (messagingexception ex) {ex. printstacktrace ();} return false ;} /*** // *** send an email in HTML format * @ Param mailinfo the email to be sent */public static Boolean sendhtmlmail (mailsenderinfo mailinfo) {// determine whether identity authentication is required. myauthenticator authenticator = NULL; properties pro = mailinfo. getproperties (); // If identity authentication is required, create a password validator if (mailinfo. isvalidate () {authenticator = new myauthenticator (mailinfo. getUserName (), mailinfo. getPassword ();} // construct a mail sending session sendmailsession = session based on the mail session attribute and password validators. getdefaultinstance (Pro, Authenticator); try {// create an email message mailmessage = new mimemessage (sendmailsession) based on the session; // create the mail sender address from = new internetaddress (mailinfo. getfromaddress (); // sets the sender of the email message mailmessage. setfrom (from); // create the recipient address of the email and set it to address to = new internetaddress (mailinfo. gettoaddress (); // message. recipienttype. the to attribute indicates that the receiver type is to mailmessage. setrecipient (message. recipienttype. to, to); // set the subject of the email message mailmessage. setsubject (mailinfo. getsubject (); // set the mail message sending time mailmessage. setsentdate (new date (); // The minimultipart class is a container class that contains the multipart mainpart = new mimemultipart (); // create a mimebodypart bodypart HTML containing HTML content = new mimebodypart (); // set the HTML content in HTML. setcontent (mailinfo. getcontent (), "text/html; charset = UTF-8"); mainpart. addbodypart (HTML); // set the minimultipart object to mailmessage. setcontent (mainpart); // send the email transport. send (mailmessage); Return true;} catch (messagingexception ex) {ex. printstacktrace () ;}return false ;}} package COM. util. mail; import javax. mail. *; public class myauthenticator extends authenticator {string username = NULL; string Password = NULL; Public myauthenticator () {} public myauthenticator (string username, string password) {This. username = username; this. password = password;} protected passwordauthentication getpasswordauthentication () {return New passwordauthentication (username, password) ;}} public static void main (string [] ARGs) {// This class is mainly used to set mailsenderinfo mailinfo = new mailsenderinfo (); mailinfo. setmailserverhost ("smtp.163.com"); mailinfo. setmailserverport ("25"); mailinfo. setvalidate (true); mailinfo. setusername ("han2000lei@163.com"); mailinfo. setpassword ("***********"); // your email password mailinfo. setfromaddress ("han2000lei@163.com"); mailinfo. settoaddress ("han2000lei@163.com"); mailinfo. setsubject ("set the mailbox title such as http://www.guihua.org China Osmanthus network"); mailinfo. setcontent ("set mailbox content such as http://www.guihua.org China Osmanthus network is China's largest Osmanthus website ="); // This class mainly comes to send mail simplemailsender SMS = new simplemailsender (); SMS. sendtextmail (mailinfo); // The text style of SMS. sendhtmlmail (mailinfo); // send HTML format}