过去,我在程序中多次使用 javax.mail 发送电子邮件,没有任何问题。我使用的版本是1.4.5
现在我已经从这里下载了该库的最新版本(1.6.5 - 现在称为 jakarta.mail):eclipse-ee4j.github.io/mail,但我在使用它时遇到问题。这是我的代码(更改库时它不会更改):
Properties properties = new Properties();
properties.put("mail.transport.protocol", "smtp");
properties.put("mail.smtp.host", host);
properties.put("mail.smtp.port", port);
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "false");
Authenticator authenticator = new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
};
Session session = Session.getDefaultInstance(properties, authenticator);
session.setDebug(true);
MimeMessage message = new MimeMessage(session);
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(cc));
message.setFrom(new InternetAddress(sender, senderName));
message.setSubject(oggetto);
message.setText(testo);
Transport.send(message);
这是我使用“新库”发送消息时得到的结果(我已激活 session 调试):
DEBUG: setDebug: Jakarta Mail version 1.6.5
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=10.255.59.13, user=mmiorandi, password=<null>
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "10.255.59.13", port 25, isSSL false
220 vinfosynapp01 ESMTP
DEBUG SMTP: connected to host "10.255.59.13", port: 25
EHLO IS01.Infosyn.com
250-vinfosynapp01
250-SIZE
250-AUTH LOGIN
250 HELP
DEBUG SMTP: Found extension "SIZE", arg ""
DEBUG SMTP: Found extension "AUTH", arg "LOGIN"
DEBUG SMTP: Found extension "HELP", arg ""
DEBUG SMTP: protocolConnect login, host=10.255.59.13, user=smtp@infosyn4.loc, password=<non-null>
DEBUG SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM XOAUTH2
DEBUG SMTP: Using mechanism LOGIN
DEBUG SMTP: AUTH LOGIN command trace suppressed
DEBUG SMTP: AUTH LOGIN failed
javax.mail.AuthenticationFailedException: 535 Authentication failed. Restarting authentication process.
at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:947)
at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:858)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:762)
at javax.mail.Service.connect(Service.java:364)
at javax.mail.Service.connect(Service.java:222)
at javax.mail.Service.connect(Service.java:171)
at javax.mail.Transport.send0(Transport.java:230)
at javax.mail.Transport.send(Transport.java:100)
...
这是我使用“旧”的时候得到的:
DEBUG: setDebug: JavaMail version 1.4.5
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "10.255.59.13", port 25, isSSL false
220 vinfosynapp01 ESMTP
DEBUG SMTP: connected to host "10.255.59.13", port: 25
EHLO IS01.Infosyn.com
250-vinfosynapp01
250-SIZE
250-AUTH LOGIN
250 HELP
DEBUG SMTP: Found extension "SIZE", arg ""
DEBUG SMTP: Found extension "AUTH", arg "LOGIN"
DEBUG SMTP: Found extension "HELP", arg ""
DEBUG SMTP: Attempt to authenticate
DEBUG SMTP: check mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM
DEBUG SMTP: AUTH LOGIN command trace suppressed
DEBUG SMTP: AUTH LOGIN succeeded
...
我看到的第一个区别是“检查机制”:LOGIN PLAIN DIGEST-MD5 NTLM XOAUTH2 与 LOGIN PLAIN DIGEST-MD5 NTLM。 难道这就是问题所在吗?
最佳答案
当 Store 或 Transport 对象上的 connect 方法由于身份验证失败(例如,错误的用户名或密码)而失败时,会引发此异常。
尝试将端口更改为587。
也试试这个。
Session session = Session.getInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, password);
}
});
此外,如果您使用基于 Google 的网络邮件,则需要确保禁用安全性较低的应用程序。只需单击下面的链接并禁用安全设置即可。
关于java - 使用 javax.mail 发送电子邮件 : the newer version of the library brings unexpected errors,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60738995/