如何解决 javax.mail.AuthenticationFailedException 问题?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2047942/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to resolve javax.mail.AuthenticationFailedException issue?
提问by jl.
I am doing a sendMail
Servlet
with JavaMail
. I have javax.mail.AuthenticationFailedException
on my output. Can anyone please help me out? Thanks.
我在做一个sendMail
Servlet
用JavaMail
。我有javax.mail.AuthenticationFailedException
我的输出。任何人都可以帮我吗?谢谢。
sendMailServlet code:
sendMailServlet 代码:
try {
String host = "smtp.gmail.com";
String from = "[email protected]";
String pass = "pass";
Properties props = System.getProperties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", from);
props.put("mail.smtp.password", pass);
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
props.put("mail.debug", "true");
Session session = Session.getDefaultInstance(props, null);
MimeMessage message = new MimeMessage(session);
Address fromAddress = new InternetAddress(from);
Address toAddress = new InternetAddress("[email protected]");
message.setFrom(fromAddress);
message.setRecipient(Message.RecipientType.TO, toAddress);
message.setSubject("Testing JavaMail");
message.setText("Welcome to JavaMail");
Transport transport = session.getTransport("smtp");
transport.connect(host, from, pass);
message.saveChanges();
Transport.send(message);
transport.close();
}catch(Exception ex){
out.println("<html><head></head><body>");
out.println("ERROR: " + ex);
out.println("</body></html>");
}
Output on GlassFish 2.1:
GlassFish 2.1 上的输出:
DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 587, isSSL false
220 mx.google.com ESMTP 36sm10907668yxh.13
DEBUG SMTP: connected to host "smtp.gmail.com", port: 587
EHLO platform-4cfaca
250-mx.google.com at your service, [203.126.159.130]
250-SIZE 35651584
250-8BITMIME
250-STARTTLS
250-ENHANCEDSTATUSCODES
250 PIPELINING
DEBUG SMTP: Found extension "SIZE", arg "35651584"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "PIPELINING", arg ""
STARTTLS
220 2.0.0 Ready to start TLS
EHLO platform-4cfaca
250-mx.google.com at your service, [203.126.159.130]
250-SIZE 35651584
250-8BITMIME
250-AUTH LOGIN PLAIN
250-ENHANCEDSTATUSCODES
250 PIPELINING
DEBUG SMTP: Found extension "SIZE", arg "35651584"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN"
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Attempt to authenticate
AUTH LOGIN
334 VXNlcm5hbWU6
aWpveWNlbGVvbmdAZ21haWwuY29t
334 UGFzc3dvcmQ6
MTIzNDU2Nzhf
235 2.7.0 Accepted
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
采纳答案by n002213f
You need to implement a custom Authenticator
您需要实现自定义 Authenticator
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
class GMailAuthenticator extends Authenticator {
String user;
String pw;
public GMailAuthenticator (String username, String password)
{
super();
this.user = username;
this.pw = password;
}
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(user, pw);
}
}
Now use it in the Session
现在在 Session
Session session = Session.getInstance(props, new GMailAuthenticator(username, password));
Also check out the JavaMail FAQ
另请查看JavaMail 常见问题解答
回答by Aaqib
I was missing this authenticator object argument in the below line
我在下面的行中缺少此身份验证器对象参数
Session session = Session.getInstance(props, new GMailAuthenticator(username, password));
This line solved my problem now I can send mail through my Java application. Rest of the code is simple just like above.
这条线解决了我的问题,现在我可以通过我的 Java 应用程序发送邮件。其余的代码就像上面一样简单。
回答by ThePCWizard
The problem is, you are creating a transport
object and using it's connect method to authenticate yourself.
But then you use a static
method to send the message which ignores authentication done by the object.
问题是,您正在创建一个transport
对象并使用它的 connect 方法来验证自己。但是然后您使用一种static
方法来发送忽略对象完成的身份验证的消息。
So, you should either use the sendMessage(message, message.getAllRecipients())
method on the object or use an authenticator as suggested by others to get authorize
through the session.
因此,您应该sendMessage(message, message.getAllRecipients())
在对象上使用该方法,或者使用其他人建议的身份验证器来通过会话获得授权。
Here's the Java Mail FAQ, you need to read.
这是Java 邮件常见问题解答,您需要阅读。
回答by Zack S
Just wanted to share with you:
I happened to get this error after changing Digital Ocean machine (IP address). Apparently Gmail recognized it as a hacking attack. After following their directions, and approving the new IP address the code is back and running.
只是想和大家分享一下:
我在更改Digital Ocean机器(IP地址)后碰巧出现此错误。显然,Gmail 将其识别为黑客攻击。按照他们的指示并批准新的 IP 地址后,代码将重新运行。
回答by UmaShankar
This error is from google security... This Can Be Resolved by Enabling Less Secure .
此错误来自 google security... 这可以通过启用 Less Secure 来解决。
Go To This Link : "https://www.google.com/settings/security/lesssecureapps" and Make "TURN ON" then your application runs For Sure.
转到此链接:“ https://www.google.com/settings/security/lesssecureapps”并“打开”,然后您的应用程序肯定会运行。
回答by dasunse
When you are trying to sign in to your Google Account from your new device or application you have to unlock the CAPTCHA.
To unlock the CAPTCHA go to https://www.google.com/accounts/DisplayUnlockCaptchaand then
当您尝试从新设备或应用程序登录您的 Google 帐户时,您必须解锁 CAPTCHA。要解锁验证码,请访问https://www.google.com/accounts/DisplayUnlockCaptcha,然后
And also make sure to allow less secure apps on
还要确保允许不太安全的应用程序