1 jar Package
Email.jar package, download online
2 Source code
Packagezjr.amy.emil.test;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.MimeMessage;/*** e-mail Test *@authorZhujinrong **/ Public classMailTest {StaticAuthenticator auth =NewAuthenticator () {@Overrideprotectedpasswordauthentication getpasswordauthentication () {//TODO auto-generated Method Stub return NewPasswordauthentication ("[Email protected]", "Password"); } }; /*** Main function *@paramargs*/ Public Static voidMain (string[] args)throwsmessagingexception {//TODO auto-generated Method StubProperties props =NewProperties (); Props.put ("Mail.smtp.host", "smtp.qq.com"); Props.put ("Mail.smtp.auth", "true"); Props.put ("Mail.from", "[email protected]"); Session Session=session.getinstance (props, auth); MimeMessage msg=NewMimeMessage (session); Msg.setfrom (); Msg.setrecipients (Message.RecipientType.TO,"[Email protected]"); Msg.setsubject ("JavaMail Hello World Example"); Msg.setsentdate (NewDate ()); Msg.settext ("); Transport.send (msg); }}
3 Running Results
4 Errors encountered
Exception in thread "main" javax.mail.authenticationfailedexception:454 authentication failed, please open SMTP flag firs T!At com.sun.mail.smtp.smtptransport$authenticator.authenticate (Smtptransport.java:826) at Com.sun.mail.smtp.SMTPTransport.authenticate (Smtptransport.java:761) at Com.sun.mail.smtp.SMTPTransport.protocolConnect (Smtptransport.java:685) at Javax.mail.Service.connect (Service.java:317) at Javax.mail.Service.connect (Service.java:176) at Javax.mail.Service.connect (Service.java:125) at Javax.mail.Transport.send0 (Transport.java:194) at Javax.mail.Transport.send (Transport.java:124) at Zjr.amy.emil.test.MailTest.main (Mailtest.java:46)
Reason: No QQ mailbox does not have SMTP service turned on
Solution: In the QQ mailbox settings inside, find the account-"POP3/IMAP/SMTP Select Open POP3/SMTP Service
5 git address
Git address: https://github.com/HelloAmy/JavaStudy.git
Java sends mail with Email.jar