Java sends mail with Email.jar

Source: Internet
Author: User

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

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.