解决错误javax.mail.AuthenticationFailedException

我不熟悉这个函数在java中发送邮件。 发送电子邮件重置密码时出现错误。 希望你能给我一个解决scheme。

以下是我的代码:

public synchronized static boolean sendMailAdvance(String emailTo, String subject, String body) { String host = AppConfigManager.getProperty("SENDER-EMAIL-SMTP-ADDRESS"); String userName = AppConfigManager.getProperty("SENDER-EMAIL-SMTP-USERNAME"); String password = AppConfigManager.getProperty("SENDER-EMAIL-SMTP-PASSWORD"); String port = AppConfigManager.getProperty("SENDER-EMAIL-SMTP-PORT"); String starttls = AppConfigManager.getProperty("SENDER-EMAIL-SMTP-STARTTLS"); String socketFactoryClass = AppConfigManager.getProperty("SENDER-EMAIL-SMTP-SOCKET-CLASS"); String fallback = AppConfigManager.getProperty("SENDER-EMAIL-SMTP-ALLOW-FALLBACK"); try { java.util.Properties props = null; props = System.getProperties(); props.put("mail.smtp.user", userName); props.put("mail.smtp.host", host); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.debug", "true"); if(!"".equals(port)) { props.put("mail.smtp.port", port); props.put("mail.smtp.socketFactory.port", port); } if(!"".equals(starttls)) props.put("mail.smtp.starttls.enable",starttls); if(!"".equals(socketFactoryClass)) props.put("mail.smtp.socketFactory.class",socketFactoryClass); if(!"".equals(fallback)) props.put("mail.smtp.socketFactory.fallback", fallback); Session session = Session.getDefaultInstance(props, null); session.setDebug(true); MimeMessage msg = new MimeMessage(session); msg.setFrom(new InternetAddress(userName)); msg.setSubject(subject); msg.setText(body, "ISO-8859-1"); msg.setSentDate(new Date()); msg.setHeader("content-Type", "text/html;charset=\"ISO-8859-1\""); msg.addRecipient(Message.RecipientType.TO, new InternetAddress(emailTo)); msg.saveChanges(); Transport transport = session.getTransport("smtp"); transport.connect(host, userName, password); transport.sendMessage(msg, msg.getAllRecipients()); transport.close(); return true; } catch (Exception mex) { mex.printStackTrace(); return false; } } 

抛出以下错误:

 DEBUG: setDebug: JavaMail version 1.4.1ea-SNAPSHOT DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc] DEBUG SMTP: useEhlo true, useAuth true DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 465, isSSL false 220 mx.google.com ESMTP m4sm5929870pbg.38 - gsmtp DEBUG SMTP: connected to host "smtp.gmail.com", port: 465 EHLO fatin 250-mx.google.com at your service, [175.139.198.14] 250-SIZE 35882577 250-8BITMIME 250-AUTH LOGIN PLAIN XOAUTH XOAUTH2 PLAIN-CLIENTTOKEN 250-ENHANCEDSTATUSCODES 250 CHUNKING DEBUG SMTP: Found extension "SIZE", arg "35882577" DEBUG SMTP: Found extension "8BITMIME", arg "" DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN XOAUTH XOAUTH2 PLAIN-CLIENTTOKEN" DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg "" DEBUG SMTP: Found extension "CHUNKING", arg "" DEBUG SMTP: Attempt to authenticate AUTH LOGIN 334 VXNlcm5hbWU6 YWNjb3VudEBibG9vbWluZy5jb20ubXk= 334 UGFzc3dvcmQ6 Ymxvb21pbmc= 535-5.7.8 Username and Password not accepted. Learn more at 535 5.7.8 http://support.google.com/mail/bin/answer.py?answer=14257 m4sm5929870pbg.38 - gsmtp [STDOUT] javax.mail.AuthenticationFailedException [STDOUT] at javax.mail.Service.connect(Service.java:319) [STDOUT] at javax.mail.Service.connect(Service.java:169) [STDOUT] at com.vlee.util.mail.SendMail.sendMailAdvance(SendMail.java:283) [STDOUT] at com.vlee.servlet.ecommerce.DoMemberLogin.fnSendPwd(DoMemberLogin.java:251) [STDOUT] at com.vlee.servlet.ecommerce.DoMemberLogin.doPost(DoMemberLogin.java:72) 

你应该改变端口为587 ,我testing了你的代码,它工作正常

如果错误仍然存​​在,请将会话variables更改为以下代码:

 Session session = Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(userName, password); } }); 

可能是由Gmail帐户保护导致的这个问题。 只需点击下面的链接,并禁用安全settings.It将工作。 https://www.google.com/settings/security/lesssecureapps

大多数AuthenticationFieldException错误发生在login尝试被阻止时,首先login您的gmail并转到https://www.google.com/settings/security/lesssecureapps并打开。; 我这样解决了这样的问题。

默认情况下,Gmail帐户是高度安全的。 当我们使用gmail smtp从非Gmail工具,电子邮件被阻止。 要在我们的本地环境中进行testing,请确保您的Gmail帐户不太安全

  1. login到Gmail。
  2. https://www.google.com/settings/security/lesssecureapps访问该url
  3. select“打开”

更改(设置不太安全的应用): https : //www.google.com/settings/security/lesssecureapps

2个可能的原因:

  • 您的用户名可能需要整个电子邮件ID“username@example.com”
  • 最明显的是:密码错误。 debugging以查看正在使用的密码是否正确。

我长期以来一直在得到同样的错误。

当我将会话debugging更改为true

 Session session = Session.getDefaultInstance(props, new GMailAuthenticator("xxxxx@gmail.com", "xxxxx")); session.setDebug(true); 

我从控制台和javax.mail.AuthenticationFailedException中获得了帮助urlhttps://support.google.com/mail/answer/78754 。

从链接中的步骤,我跟着每一步。 当我改变我的密码混合的字母,数字和符号是我的惊喜电子邮件生成没有身份validation例外。

注意:我的旧密码更安全。

如果您从新的应用程序或设备login到您的Gmail帐户,Google可能会阻止该设备。 请尝试以下步骤:

为了保护您的帐户,如果我们怀疑您的帐户不是您,Google可能会更难以login您的帐户。 例如,如果您正在旅行或尝试从新设备login您的帐户,Google可能会要求您提供除用户名和密码外的其他信息。

从以前用于访问您的Google帐户的其他设备转到https://g.co/allowaccess ,然后按照说明进行操作。 尝试再次从被封锁的应用程序login。

 trying to connect to host "smtp.gmail.com", port 465, isSSL false 

你有你的Gmail的SMTP设置错误。 Gmail需要SSL。 请参阅关于如何通过Gmail SMTP通过Java发送电子邮件的教程,例如: http : //www.mkyong.com/java/javamail-api-sending-email-via-gmail-smtp-example/

 import java.util.*; import javax.mail.*; import javax.mail.internet.*; import javax.activation.*; public class SendMail1 { public static void main(String[] args) { // Recipient's email ID needs to be mentioned. String to = "valid email to address"; // Sender's email ID needs to be mentioned String from = "valid email from address"; // Get system properties Properties properties = System.getProperties(); properties.put("mail.smtp.starttls.enable", "true"); properties.put("mail.smtp.host", "smtp.gmail.com"); properties.put("mail.smtp.port", "587"); properties.put("mail.smtp.auth", "true"); Authenticator authenticator = new Authenticator () { public PasswordAuthentication getPasswordAuthentication(){ return new PasswordAuthentication("userid","password");//userid and password for "from" email address } }; Session session = Session.getDefaultInstance( properties , authenticator); try{ // Create a default MimeMessage object. MimeMessage message = new MimeMessage(session); // Set From: header field of the header. message.setFrom(new InternetAddress(from)); // Set To: header field of the header. message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); // Set Subject: header field message.setSubject("This is the Subject Line!"); // Now set the actual message message.setText("This is actual message"); // Send message Transport.send(message); System.out.println("Sent message successfully...."); }catch (MessagingException mex) { mex.printStackTrace(); } } } 

以防万一谁来寻找这个问题的解决scheme。

authentication问题可以通过激活所使用帐户的Google两步validation并创build应用专用密码来缓解。 我有和OP一样的问题。 启用两步工作。

我也有这个问题,但解决scheme与编码无关。 确保你能够连接到Gmail。 Ping smtp.gmail.com。 如果您没有得到答复,请检查您的防火墙设置。 这也可能是一个代理设置问题。