![从 java servlet 发送邮件身份验证失败](https://linux22.com/image/755015/%E4%BB%8E%20java%20servlet%20%E5%8F%91%E9%80%81%E9%82%AE%E4%BB%B6%E8%BA%AB%E4%BB%BD%E9%AA%8C%E8%AF%81%E5%A4%B1%E8%B4%A5.png)
Centos7、tomcat 9、Exim 4.94
将我的所有 tomcat 应用程序从 centos6 移至 centos7 服务器。Tomcat 版本为 9,两台服务器上的版本相同。
我从其中一个 Java 应用程序发送电子邮件。
public void sendEmail(String toAddress,
String subject, String message) throws AddressException,
MessagingException {
// sets SMTP server properties
Properties properties = new Properties();
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");
// creates a new session with an authenticator
Authenticator auth = new Authenticator() {
@Override
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, pass);
}
};
Session session = Session.getInstance(properties, auth);
// creates a new e-mail message
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(user));
InternetAddress[] toAddresses = { new InternetAddress(toAddress) };
msg.setReplyTo(new javax.mail.Address[] {new javax.mail.internet.InternetAddress(replyTo)});
msg.setRecipients(Message.RecipientType.TO, toAddresses);
msg.setSubject(subject);
msg.setSentDate(new Date());
msg.setText(message);
// sends the e-mail
Transport.send(msg);
}
变量主机 = 我的 IP,端口 = 25
从命令行发送工作正常
mail -s“测试主题”[电子邮件保护]< /dev/null
从 servlet 发送时,我在 catalina.out 中收到此错误
javax.mail.AuthenticationFailedException: 535 Incorrect authentication data
at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:826)
at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:761)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:685)
at javax.mail.Service.connect(Service.java:317)
at javax.mail.Service.connect(Service.java:176)
at javax.mail.Service.connect(Service.java:125)
at javax.mail.Transport.send0(Transport.java:194)
at javax.mail.Transport.send(Transport.java:124)
at util.EmailUtility.sendEmail(EmailUtility.java:86)
最后一行 util.EmailUtility.sendEmail(EmailUtility.java:86) 是对上述函数的调用。
我正在使用 sendmail,因为它是在此服务器上设置的。
编辑1.
我在上面的 java 应用程序函数 sendEmail 中设置了 session.setDebug(true),现在我在 catalina.out 中看到了这一点
DEBUG: setDebug: JavaMail version 1.4.7
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "68.169.63.242", port 25, isSSL false
220 www.mbepapers.org ESMTP Exim 4.94 Wed, 15 Jul 2020 14:27:07 -0400
DEBUG SMTP: connected to host "68.169.63.242", port: 25
EHLO www.mbepapers.org
250-www.mbepapers.org Hello mbepapers.org [68.169.63.242]
250-SIZE 52428800
250-8BITMIME
250-PIPELINING
250-X_PIPE_CONNECT
250-AUTH PLAIN LOGIN CRAM-MD5
250-CHUNKING
250-STARTTLS
250 HELP
DEBUG SMTP: Found extension "SIZE", arg "52428800"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "X_PIPE_CONNECT", arg ""
DEBUG SMTP: Found extension "AUTH", arg "PLAIN LOGIN CRAM-MD5"
DEBUG SMTP: Found extension "CHUNKING", arg ""
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "HELP", arg ""
DEBUG SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM
DEBUG SMTP: AUTH LOGIN command trace suppressed
DEBUG SMTP: AUTH LOGIN failed
javax.mail.AuthenticationFailedException: 535 Incorrect authentication data
谢谢
答案1
这确实很尴尬。当我设置新服务器时,我没有设置我的邮件服务器域名,这是导致此错误的原因之一。
535 身份验证数据不正确
通过控制面板添加邮件域后,它开始按预期工作......