javax.mail.AuthenticationFailedException:未能连接,没有指定密码? [英] javax.mail.AuthenticationFailedException: failed to connect, no password specified?
问题描述
此程序尝试发送电子邮件,但会引发运行时异常:
javax.mail.AuthenticationFailedException:failed to连接,没有指定密码?
当我提供正确的用户名和密码进行验证时,为什么会收到此异常?
发件人和收件人都有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);
//使用TLS
props.put(mail.smtp.auth,true);
props.put(mail.smtp.starttls.enable,true);
props.put(mail.smtp.password,password);
//使用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 =me@gmail.com;
来自=from@gmail.com的字符串;
String subject =测试...;
Message msg = new MimeMessage(session);
尝试{
msg.setFrom(new InternetAddress(from));
msg.setRecipient(Message.RecipientType.TO,
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);
$ p $即使在给出密码I得到例外。为什么不进行身份验证?尝试创建一个javax.mail.Authenticator对象,并使用properties对象发送它到Session对象。
身份验证器
编辑:
您可以修改它以接受用户名和密码,并且可以将它们存储在那里或任何你想要的地方。 / p>
public class SmtpAuthenticator extends Authenticator {
public SmtpAuthenticator(){
super();
}
@Override
public PasswordAuthentication getPasswordAuthentication(){
String username =user;
字符串密码=密码; ((username!= null)&&(username.length()> 0)&&(password!= null)
&&(password.length() > 0)){
返回新的PasswordAuthentication(用户名,密码);
}
返回null;
$ / code>
在您发送电子邮件的班级中:
SmtpAuthenticator authentication = new SmtpAuthenticator();
javax.mail.Message msg = new MimeMessage(Session
.getDefaultInstance(emailProperties,authenticator));
This program attempts to send e-mail but throws a run time exception:
javax.mail.AuthenticationFailedException: failed to connect, no password specified?
Why am I getting this exception when I have supplied the correct username and password for authentication?
Both the sender and receiver have g-mail accounts. The sender and the receiver both have g-mail accounts. The sender has 2-step verification process disabled.
This is the code:
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 = "me@gmail.com";
String from = "from@gmail.com";
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);
}
}
}
Even after giving the password I get the exception. Why is it not authenticating?
解决方案 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.
public class SmtpAuthenticator extends Authenticator {
public SmtpAuthenticator() {
super();
}
@Override
public PasswordAuthentication getPasswordAuthentication() {
String username = "user";
String password = "password";
if ((username != null) && (username.length() > 0) && (password != null)
&& (password.length () > 0)) {
return new PasswordAuthentication(username, password);
}
return null;
}
In your class where you send the email:
SmtpAuthenticator authentication = new SmtpAuthenticator();
javax.mail.Message msg = new MimeMessage(Session
.getDefaultInstance(emailProperties, authenticator));
这篇关于javax.mail.AuthenticationFailedException:未能连接,没有指定密码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!