javax.mail.AuthenticationFailedException: 無法連接,未指定口令?
2022-09-02 09:13:33
此程序尝试发送电子邮件,但引发运行时异常:
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 = "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);
}
}
}
即使在提供密码后,我也会遇到异常。为什么不进行身份验证?