解决错误javax.mail.AuthenticationFailedException [英] Solve error javax.mail.AuthenticationFailedException
问题描述
我不熟悉这个用java发送邮件的功能。发送电子邮件重置密码时收到错误。希望你能给我一个解决方案。
I'm not familiar with this function to send mail in java. I'm getting an error while sending email to reset a password. Hope you can give me a solution.
以下是我的代码:
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版本1.4.1ea-SNAPSHOT
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: useEhlo true, useAuth true
DEBUG SMTP:尝试连接主机smtp.gmail.com,端口465,isSSL false 220 mx.google.com ESMTP m4sm5929870pbg.38 - gsmtp
DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 465, isSSL false 220 mx.google.com ESMTP m4sm5929870pbg.38 - gsmtp
DEBUG SMTP:连接到主机smtp.gmail.com,端口:465
DEBUG SMTP: connected to host "smtp.gmail.com", port: 465
EHLO fatin
EHLO fatin
250-mx.google.com为您服务,[175.139.198.14]
250-mx.google.com at your service, [175.139.198.14]
250-SIZE 35882577
250-SIZE 35882577
250-8BITMIME
250-8BITMIME
250 -AUTH LOGIN PLAIN XOAUTH XOAUTH2 PLAIN-CLIENTTOKEN
250-AUTH LOGIN PLAIN XOAUTH XOAUTH2 PLAIN-CLIENTTOKEN
250-ENHANCEDSTATUSCODES
250-ENHANCEDSTATUSCODES
250 CHUNKING
250 CHUNKING
DEBUG SMTP:F扩展名SIZE,arg35882577
DEBUG SMTP: Found extension "SIZE", arg "35882577"
DEBUG SMTP:找到扩展名8BITMIME,arg
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP:找到扩展名AUTH,argLOGIN PLAIN XOAUTH XOAUTH2 PLAIN-CLIENTTOKEN
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN XOAUTH XOAUTH2 PLAIN-CLIENTTOKEN"
DEBUG SMTP:找到扩展名ENHANCEDSTATUSCODES,arg
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP:找到扩展名CHUNKING,arg
DEBUG SMTP: Found extension "CHUNKING", arg ""
DEBUG SMTP:尝试进行身份验证
DEBUG SMTP: Attempt to authenticate
AUTH LOGIN
AUTH LOGIN
334 VXNlcm5hbWU6
334 VXNlcm5hbWU6
YWNjb3VudEBibG9vbWluZy5jb20ubXk =
YWNjb3VudEBibG9vbWluZy5jb20ubXk=
334 UGFzc3dvcmQ6
334 UGFzc3dvcmQ6
Ymxvb21pbmc =
535-5.7.8不接受用户名和密码。了解详情
535 5.7.8 http://支持.google.com / mail / bin / answer.py?answer = 14257
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
m4sm5929870pbg.38 - gsmtp
[STDOUT] javax.mail.AuthenticationFailedException
[STDOUT] javax.mail.AuthenticationFailedException
[STDOUT]在javax.mail.Service.connect(Service.java:319)
[STDOUT] at javax.mail.Service.connect(Service.java:319)
[STDOUT]在javax.mail.Service.connect(Service.java:169)
[STDOUT] at javax.mail.Service.connect(Service.java:169)
[STDOUT] at com。 com.vlee.servlet.ecommerce.DoMemberLogin.fnSendPwd(DoMemberLogin.java)中的vlee.util.mail.SendMail.sendMailAdvance(SendMail.java:283)
[STDOUT] at com.vlee.util.mail.SendMail.sendMailAdvance(SendMail.java:283)
[STDOUT] :251)
[STDOUT] at com.vlee.servlet.ecommerce.DoMemberLogin.fnSendPwd(DoMemberLogin.java:251)
[STDOUT] at com.vlee.servlet.ecommerce.DoMemberLogin.doPost(DoMemberLogin.java:72)
[STDOUT] at com.vlee.servlet.ecommerce.DoMemberLogin.doPost(DoMemberLogin.java:72)
推荐答案
您应该将端口更改为 587
,我测试了您的代码,它是工作正常
You should change the port to 587
, I tested your code and it's working fine
如果仍然出现错误,请更改sessio n变量代码如下:
If error still happens, please change session variable to code below:
Session session = Session.getInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, password);
}
});
这篇关于解决错误javax.mail.AuthenticationFailedException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!