javax.mail.AuthenticationFailedException:连接失败,未指定密码?
该程序尝试发送电子邮件,但引发运行时异常:
javax.mail.AuthenticationFailedException: failed to connect, no password specified?
当我提供了正确的用户名和密码进行身份验证时,为什么会出现此异常?
发件人和收件人都有 g-mail 帐户。发件人和收件人都有 g-mail 帐户。发件人已禁用两步验证过程。
这是代码:
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
class tester {
public static void main(String args[]) {
Properties props = new Properties();
props.put("mail.smtp.host" , "smtp.gmail.com");
props.put("mail.stmp.user" , "username");
//To use TLS
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.password", "password");
//To use SSL
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Session session = Session.getDefaultInstance( props , null);
String to = "[email protected]";
String from = "[email protected]";
String subject = "Testing...";
Message msg = new MimeMessage(session);
try {
msg.setFrom(new InternetAddress(from));
msg.setRecipient(Message.RecipientType.TO,
new InternetAddress(to));
msg.setSubject(subject);
msg.setText("Working fine..!");
Transport transport = session.getTransport("smtp");
transport.connect("smtp.gmail.com" , 465 , "username", "password");
transport.send(msg);
System.out.println("fine!!");
}
catch(Exception exc) {
System.out.println(exc);
}
}
}
即使在给出密码后我也会得到异常。为什么不认证?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
尝试创建一个 javax.mail.Authenticator 对象,并将其与属性对象一起发送到 Session 对象。
身份验证器
编辑:
您可以修改它以接受用户名和密码,并且可以将它们存储在那里或任何您想要的地方。
在您发送电子邮件的班级中:
Try to create an javax.mail.Authenticator Object, and send that in with the properties object to the Session object.
Authenticator
edit:
You can modify this to accept a username and password and you can store them there, or where ever you want.
In your class where you send the email:
您需要将对象身份验证作为参数添加到会话中。比如
现在你不会得到这种异常......
You need to add the Object Authentication as the Parameter to the Session. such as
now You will not get this kind of Exception....
应为您的电子邮件会话提供一个身份验证器实例,如下所示,
完整示例位于此处 http://bharatonjava.wordpress.com/2012/08/27/sending-email-using-java-mail-api/
Your email session should be provided an authenticator instance as below
a complete example is here http://bharatonjava.wordpress.com/2012/08/27/sending-email-using-java-mail-api/
我已经在
Transport.send
调用中添加用户和密码解决了这个问题:根据 javax.mail 中的
发送
函数(来自 版本 1.5):public static void send(Message msg、字符串用户、字符串密码)
另外,如果您使用此签名,则无需设置任何
身份验证器
,也无需在属性
中设置用户和密码>(仅需要主机)。所以你的代码可能是:I've solved this issue adding user and password in
Transport.send
call:According to this signature of the
send
function in javax.mail (from version 1.5):public static void send(Message msg, String user, String password)
Also, if you use this signature it's not necessary to set up any
Authenticator
, and to set user and password in theProperties
(only the host is needed). So your code could be:除了RMT的回答。我还必须稍微修改一下代码。
这是我的示例 send() 方法。配置对象只是一个愚蠢的数据容器。
In addition to RMT's answer. I also had to modify the code a bit.
here is my sample send() methods. The config object is just a dumb data container.
首先,在您的 Gmail 帐户中启用安全性较低的应用程序,您将使用此链接从该帐户发送电子邮件:- https://myaccount.google.com/lesssecureapps?pli=1
然后,您只需在会话创建中添加以下代码即可。那么它就会工作得很好。
如果您想要更详细的信息,请使用以下内容:-
希望!这有帮助。谢谢!
First of all, enable the less secure app in your Gmail account from which you will send emails using this link:- https://myaccount.google.com/lesssecureapps?pli=1
Then you simply add the following code in your session creation. It will work fine then.
if you want the more elaborated one use the following:-
Hope! it helps. Thanks!
可能值得验证 gmail 帐户是否由于多次登录尝试失败而被锁定,您可能需要重置密码。我和你遇到了同样的问题,结果这就是解决方案。
It might be worth verifying that the gmail account hasn't been locked out due to several unsuccessful login attempts, you may need to reset your password. I had the same problem as you, and this turned out to be the solution.
查看你的代码第9行,可能是错误;应该是:
不是
See the 9 line of your code,it may be an error; it should be:
not
即使使用身份验证器,我也必须将 mail.smtp.auth 属性设置为 true。这是一个工作示例:
Even when using an Authenticator I had to set mail.smtp.auth property to true. Here is a working example:
我也有这个问题所以不用担心。由于外部身份验证问题,它来自邮件服务器端。打开您的邮件,您将从邮件服务器收到一封邮件,告诉您启用可访问性。完成后,重试您的程序。
I also have this problem so don't worry. It comes from mail server side due to an outside authentication issue. Open your mail and you will get a mail from the mail server telling you to enable accessibility. When you have done that, retry your program.
在 gmail 帐户的安全设置中打开“访问不太安全的应用程序”。(来自邮件),请参阅以下链接以获取参考
http://www.ghacks.net/2014/07/21/gmail-starts-block-less-secure-apps-enable-access/
Turn On "Access for less secure apps" in Security setting for the gmail account.(from mail), see the below link for references
http://www.ghacks.net/2014/07/21/gmail-starts-block-less-secure-apps-enable-access/
此错误可能与密码字符有关。如果您的密码包含特殊字符,并且您将密码添加到
Transport
类方法中;例如
,或者
您可能会收到该错误。因为
Transport
类的方法可能无法处理特殊字符。如果您使用 javax.mail.PasswordAuthentication 类将用户名和密码添加到消息中,我希望您能够避免该错误;例如
This error may be about password characters. If your password contains special characters and also you add your password into
Transport
class methods;For Example
or
you may get that error. Because
Transport
class' methods may not handle special characters. If you add your username and password into your message usingjavax.mail.PasswordAuthentication
class, i hope you will escape that error;For Example
我刚刚遇到这个问题,解决方案是属性“mail.smtp.user”应该是您的电子邮件(而不是用户名)。
Gmail 用户的示例:
I have just faced this problem, and the solution is that the property "mail.smtp.user" should be your email (not username).
The example for gmail user: