java - 异常:javax.mail.AuthenticationFailedException:连接失败,没有指定密码?即使使用 PasswordAuthenticator?
问题描述
我正在制作一个发送电子邮件的 java 应用程序。
出于某种原因,javax.mail.AuthenticationFailedException
即使我有正确的用户名和密码,我也不断收到 。
我看过一些帖子说缺少密码身份验证也可能导致此异常,但我认为我也有这部分。现在我被困住了,无法弄清楚哪里出了问题。
这是我的代码:
import javax.mail.*;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
import javax.swing.*;
public class sendEmail {
private static String subject = "Test", content = "This is a test";
public sendEmail() {
}
public static void send() throws MessagingException{
Properties properties = new Properties();
properties.put("mail.smtp.auth","true");// set the authentication to true
properties.put("mail.smtp.starttls.enable","true");
properties.put("mail.smtp.host","smtp.gmail.com");
properties.put("mail.smtp.port","587");
String username = "clashroyale70532@gmail.com";
String password = "********";
Session ses = Session.getInstance(properties, new Authenticator() {
protected PasswordAuthentication getPassWordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
ses.setDebug(true);
Message message = prepareMessage(ses,username);
Transport.send(message);
}
private static Message prepareMessage(Session session, String account) {
try {
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(account));
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(account));
msg.setSubject(subject);
msg.setText(content);
return msg;
} catch (AddressException e) {
e.printStackTrace();
} catch (MessagingException m) {
throw new RuntimeException(m);
}
return null;
}
public static void main(String[] args) {
try {
sendEmail.send();
}
catch (MessagingException m) {
m.printStackTrace();
}
}
}
这是 JavaMail 调试输出:
DEBUG: setDebug: JavaMail version 1.6.2
DEBUG: getProvider() returning
javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
DEBUG SMTP: need username and password for authentication
DEBUG SMTP: protocolConnect returning false, host=smtp.gmail.com, user=*******(name not shown, it's my real name), password=<null>
它显示的用户与我在代码中使用的电子邮件帐户没有关联。我不确定它是如何得到我的真名的。
解决方案
推荐阅读
- node.js - mongodb中的动态引用不显示用户名
- python - 如何附加文件smtplib?
- c - 如何从C中动态分配的char数组中分析字符串
- groovy - 如何在 Jenkins 声明式管道中编写自定义 def 函数?
- mysql - 具有多个查找表的 GROUP_CONCAT 和 JOINS
- css - React 中的 TailWindCSS 输入字段更改
- python - python 3.8中“test_data:n_test”的含义
- python-3.x - 存储在数组/列表中的 Python 日期时间变量
- c++ - 有没有办法从设定的精度中排除一个值?
- python - 拆分一列字符串,然后按索引分组,然后将它们相加?