PackageCom.app.tools;Importjava.util.Date;Importjava.util.Properties;ImportJavax.mail.Authenticator;ImportJavax.mail.Message;Importjavax.mail.MessagingException;Importjavax.mail.PasswordAuthentication;Importjavax.mail.Session;ImportJavax.mail.Transport;Importjavax.mail.internet.InternetAddress;ImportJavax.mail.internet.MimeMessage; Public classSendEmail {//Public static final String HOST = "mx3.qq.com"; Public Static FinalString HOST = "smtp.163.com"; Public Static FinalString PROTOCOL = "SMTP"; Public Static Final intPORT = 25;/*465 This is the port of QQ*/ Public Static FinalString from = "[email protected]";//Email from sender Public Static FinalString PWD = "xxxxxxx";//Sender Password Private StaticSession getsession () {Properties props=NewProperties (); Props.put ("Mail.smtp.host", host);//Set server addressProps.put ("Mail.store.protocol", protocol);//set up ProtocolsProps.put ("Mail.smtp.port", port);//Setting up PortsProps.put ("Mail.smtp.auth", "true"); Authenticator Authenticator=NewAuthenticator () {@Overrideprotectedpasswordauthentication getpasswordauthentication () {return Newpasswordauthentication (from, PWD); } }; Session Session=session.getdefaultinstance (props, authenticator); returnsession; } Public Static voidSend (String toemail, string content) {Session session=getsession (); Try{System.out.println ("--send--" +content); //Instantiate a messageMessage msg =NewMimeMessage (session); //Set Message AttributesMsg.setfrom (Newinternetaddress (from)); Internetaddress[] Address= {Newinternetaddress (Toemail)}; Msg.setrecipients (Message.RecipientType.TO, address); Msg.setsubject ("Account activation Email"); Msg.setsentdate (NewDate ()); Msg.setcontent (Content,"Text/html;charset=utf-8"); //Send the messagetransport.send (msg); } Catch(Messagingexception Mex) {mex.printstacktrace (); } } Public Static voidMain (string[] args) {NewSendEmail (). Send ("[email protected]", "DSFSDF"); System.out.println ("Success"); }}
Please note the following points:
1, whether to open the certification, QQ mailbox, 163 mailboxes are to open the certification
2,javax.mail.MessagingException:Could not connect to SMTP host:smtp.163.com, port:25; Connection timed out
Solution Reference: Add this property to the true quotation marks, please wonderful unusual! Note: If you do not have this attribute, the exception that is likely or thrown to require an authentication exception (Required authentication) is typically caused by 1, 2
3. When setting properties, be sure to make sure that you use the address and port of the mailbox. javax.mail.AuthenticationFailedException
QQ Mailbox Port: Mx3.qq.com Port: 465 163 e-mail: smtp.163.com Port: All this is used by the SMTP protocol that is used when sending mail. If it's a POP3 protocol, it's not the same. Please find it yourself.
4.
This exception occurs when you use a QQ mailbox to send a message. Check it out, the official statement is sent too frequently so be locked up! There is no solution yet. Try it tomorrow to see if you can succeed (not successful)
The above is a few easy exceptions, after the solution, other exceptions can be based on the specific information thrown out!
Recently want to write a mailbox automatic verification function, read a lot on the net, write to yourself above a lot of problems, record down to the people behind a reference