JavaMail sends an email for publishing. javamail sends an email.
Errors are always reported when sending emails in java. The error message is javax. mail. AuthenticationFailedException.
Unable to connect to the smtp server, the reason is stmp server is not authorized, for example, you use qq mail: 927xxxxx@qq.com you must authorize the current mailbox, the authorization steps Baidu is very simple, authorization is enabled when you need to send a text message.
Transport transport = mailSession. getTransport ("smtp"); transport. connect (smtpHost, "xxxxx@163.com", "password"); // The password is the authorization code transport. sendMessage (testMessage, testMessage. getAllRecipients (); transport. close ();
In this case, the error javax. mail. AuthenticationFailedException will not be reported.
I found another error while continuing the test.
javax.mail.AuthenticationFailedException: 530 Error: A secure connection is requiered(such as ssl). More information at http://service.mail.qq.com/cgi-bin/help?id=28 at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:823) at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:756) at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:673) at javax.mail.Service.connect(Service.java:295) at javax.mail.Service.connect(Service.java:176) at com.ab.itws.common.SendHtmlMail.sendMessage(SendHtmlMail.java:43) at com.ab.itws.common.SendHtmlMail.main(SendHtmlMail.java:61)
Port problems discovered
// SMTP server port (the default port for non-SSL connection is 25, which can be left blank. If SSL connection is enabled, /// * You need to change the SMTP server port of the corresponding mailbox. For details, see the help of the corresponding mailbox service. // The SMTP (SLL) Port of the QQ mailbox is 465 or 587 ,*/
System. out. println ("processing ing mail session for:" + smtpHost );
Java. util. Properties props = new java. util. Properties ();
Props. setProperty ("mail. smtp. auth", "true"); // specifies whether SMTP verification is required.
Props. setProperty ("mail. smtp. host", smtpHost); // specifies the SMTP server
Props. put ("mail. transport. protocol", "smtp ");
Props. put ("mail. smtp. port", "587"); // SMTP port number
Session mailSession = Session. getDefaultInstance (props );
MailSession. setDebug (true); // whether the debug information is displayed on the console
After the port is changed, the problem is finally solved.
Email sent successfully