1. First create an email with the information class:
Package Com.tan.test;import java.util.properties;/** * Features: Mailbox Information class * @author TanZi * @time 2015-4-20 morning 10:35:47 */public class Mailsenderinfo {//Send mail server Authentication required information private String mailserverhost; The IP address of the server is private String mailserverport; Port number//sender's information private String formaddress; Sender's address private string username;//sender's account private string password;//sender's pndc//recipient private string toaddress;//recipient's address// The subject of the sent private string subject;//the contents of the private string content;/** * function: Get mail Session Properties * @return */public Property getProperties () {Properties Properties=new properties ();p roperties.put ("Mail.smtp.host", this.mailserverhost); Host address Properties.put ("Mail.smtp.port", This.mailserverport); Port number Properties.put ("Mail.smtp.auth", "true"); Set Send authorization authentication return properties;} ------------------getter/setter-------------------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 String getformaddress () {return formaddress;} public void setformaddress (String formaddress) {this.formaddress = formaddress;} Public String GetUserName () {return userName;} public void Setusername (String userName) {this.username = UserName;} 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 Getsubject () {return subject;} public void Setsubject (String subject) {this.subject = subject;} Public String getcontent () {return content;} public void SetContent (String content) {this.content = content;}}
2. Creating inheritance, overriding authentication methods
Package Com.tan.test;import Javax.mail.authenticator;import javax.mail.passwordauthentication;/** * Features: Authentication Class-- Verification of mailbox accounts and Passwords * @author TanZi * @time 2015-4-20 morning 10:52:17 */public class Myauthenticator extends Authenticator{string Usernam E=null; String Password=null;public myauthenticator () {}public myauthenticator (string username,string passowrd) { THIS.USERNAME=USERNAME;THIS.PASSWORD=PASSOWRD;} /** * username and password Authentication */public passwordauthentication getpasswordauthentication () {return new Passwordauthentication (username , password);}}
3. Write the outgoing email core class (How to load the data. How to send)
Package Com.tan.test;import Java.util.date;import Java.util.properties;import javax.activation.commandmap;import Javax.activation.datahandler;import Javax.activation.datasource;import Javax.activation.filedatasource;import Javax.activation.mailcapcommandmap;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.addressexception;import javax.mail.internet.InternetAddress; Import Javax.mail.internet.mimebodypart;import Javax.mail.internet.mimemessage;import javax.mail.internet.mimemultipart;/** * Features: Message sending Core class * @author TanZi * @time 2015-4-20 a.m. 11:02:26 */public class Simlemails Ender {/** * Function: The core method of message sending * @param mailinfo Message Class * @return Boolean */public boolean sendhtmlmail (Mailsenderinfo mailinfo ) {Myauthenticator authenticator=new myauthenticator (Mailinfo.getusername (), Mailinfo.getpassword ());// Authentication Properties Properties=mAilinfo.getproperties (); Constructs a sessionsession sendmailsession=session.getdefaultinstance (properties, authenticator) for sending mail based on mail session properties and validators; try { Defines the principal message mailmessage=new MimeMessage (sendmailsession) that sends the message;//creates the address of the sender Fromaddress=new internetaddress ( Mailinfo.getformaddress ());//Set the sender of the mail message mailmessage.setfrom (fromaddress);//Create the address of the recipient Toaddress=new InternetAddress (Mailinfo.gettoaddress ());//Create the recipient of the mail message mailmessage.setrecipient (Message.RecipientType.TO, toaddress);//bcc: Bcc. CC: CC. To: Direct send//email subject mailmessage.setsubject (Mailinfo.getsubject ());//Set the time Mailmessage.setsentdate (new Date ()) to send the message;// Set the main content of the mail message//string mailcontent=mailinfo.getcontent ();//Create a container multipart maimpart=new mimemultipart ();// Create a Mimebodypartbodypart html=new MimeBodyPart () that contains HTML content, or//set the message contents of the HTML Html.setcontent (Mailinfo.getcontent (), " Text/html;charset=utf-8 "); Maimpart.addbodypart (HTML);//Create Attachment BodyPart attach=new MimeBodyPart ();D Atasource source= New Filedatasource ("e:\\code\\movie\\images\\four.jpg");//load the file to be attached as an attach.Setdatahandler (source), Attach.setfilename ("four.jpg"); Maimpart.addbodypart (attach);// Set content mailmessage.setcontent (Maimpart);//Set message header information sent in HTML format mailcapcommandmap mc= (MAILCAPCOMMANDMAP) Commandmap.getdefaultcommandmap (); Mc.addmailcap ("text/html;; X-java-content-handler=com.sun.mail.handlers.text_html "); Mc.addmailcap ("text/xml;; X-java-content-handler=com.sun.mail.handlers.text_xml "); Mc.addmailcap ("text/plain;; X-java-content-handler=com.sun.mail.handlers.text_plain "); Mc.addmailcap ("multipart/*;; X-java-content-handler=com.sun.mail.handlers.multipart_mixed "); Mc.addmailcap ("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822 "); Commandmap.setdefaultcommandmap (MC); Send file Transport.send (mailmessage); return true;} catch (Addressexception e) {e.printstacktrace ()} catch (Messagingexception e) {//TODO auto-generated catch Blocke.print StackTrace ();} return false;}}
4. Test class
Package com.tan.test;/** * Features: * @author TanZi * @time 2015-4-20 pm 1:16:15 */public class Testmail {/** * function: * @param args */public static void Main (string[] args) {Mailsenderinfo mailinfo = new Mailsenderinfo (); System.out.println (""); Mailinfo.setmailserverhost ("smtp.qq.com"); Mail server host Mailinfo.setmailserverport ("25"); Server Port SMTP: Single Message Transfer Protocol port: 25mailinfo.setusername ("571868789"); Sender User name Mailinfo.setpassword ("tanliu1234"); Sender password Mailinfo.setformaddress ("[email protected]"); Sender address//mailinfo.settoaddress ("[email protected]"); Receiver address string toaddress = "[email protected]"; mailinfo.settoaddress (toaddress); String subject = "TanZi-------text"; Topic string content = "<a href= ' http://blog.csdn.net/u011707402 ' >"; Content + = "<div style= ' width:650px;background-color: #DBDBFF;"; Content + = "text-align:center;color:red;font-size:20px;border:0 solid red;" > "; Content + = "<b> Congratulations on your receipt of my message <br/>"; Content + = "<a href= ' http://blog.csdn.net/u011707402' > Jar Growth Mind Learning knowledge </a><br/> '; Content + = "jar"; Content + = "</b></div></a> "; Mailinfo.setsubject (subject); mailinfo.setcontent (content); Simlemailsender SMS = new Simlemailsender (); Boolean flag = Sms.sendhtmlmail (Mailinfo); Send message if (flag) {SYSTEM.OUT.PRINTLN ("Success! ");} ELSE{SYSTEM.OUT.PRINTLN ("Failure! ");}}}
5. Precautions
- Send Message Error 454 authentication failed, please open SMTP flag first! ----(method) in the QQ mailbox settings inside, find the account-"POP3/IMAP/SMTP choose to open POP3/SMTP service
- 535 Authentication failed (reason): Incorrect account number and password
About Java sending an email case