我试图弄清楚当 smtp.auth 属性设置为 false 时为什么会收到 AuthenticationFailedException。从调试日志中,它仍然尝试验证登录。我已经针对在服务器上运行的 localhost smtp 服务器测试了代码,该服务器运行良好,但在发送到第 3 方服务器时失败。
我已经尝试通过 telnet 手动发送电子邮件,而不使用身份验证,并且电子邮件是通过 3rd 方 smtp 服务器成功发送的,所以我不相信这是服务器的限制。
我正在使用 javamail-1.4.7。
调试输出
我不明白为什么即使它被禁用它也试图进行身份验证
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
Oct 18, 2013 11:59:25 PM javax.mail.Session getProvider
FINE: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
DEBUG SMTP: useEhlo true, useAuth false
Oct 18, 2013 11:59:25 PM com.sun.mail.smtp.SMTPTransport protocolConnect
FINE: useEhlo true, useAuth false
DEBUG SMTP: trying to connect to host "mail.server.com", port 25, isSSL false
Oct 18, 2013 11:59:25 PM com.sun.mail.smtp.SMTPTransport openServer
FINE: trying to connect to host "mail.server.com", port 25, isSSL false
Oct 18, 2013 11:59:25 PM com.sun.mail.util.SocketFetcher getSocket
FINER: getSocket, host mail.server.com, port 25, prefix mail.smtp, useSSL false
220 ded1081-lin-96-181.server.net ESMTP
Oct 18, 2013 11:59:25 PM com.sun.mail.util.LogOutputStream log
FINEST: 220 ded1081-lin-96-181.server.net ESMTP
DEBUG SMTP: connected to host "mail.server.com", port: 25
Oct 18, 2013 11:59:25 PM com.sun.mail.smtp.SMTPTransport openServer
FINE: connected to host "mail.server.com", port: 25
EHLO menez-bpm.server.com
Oct 18, 2013 11:59:25 PM com.sun.mail.util.LogOutputStream log
FINEST: EHLO menez-bpm.server.com
250-ded1081-lin-96-181.server.net
Oct 18, 2013 11:59:25 PM com.sun.mail.util.LogOutputStream log
FINEST: 250-ded1081-lin-69-81.server.net
250-AUTH=LOGIN CRAM-MD5 PLAIN
Oct 18, 2013 11:59:25 PM com.sun.mail.util.LogOutputStream log
FINEST: 250-AUTH=LOGIN CRAM-MD5 PLAIN
250-AUTH LOGIN CRAM-MD5 PLAIN
Oct 18, 2013 11:59:25 PM com.sun.mail.util.LogOutputStream log
FINEST: 250-AUTH LOGIN CRAM-MD5 PLAIN
250-STARTTLS
Oct 18, 2013 11:59:25 PM com.sun.mail.util.LogOutputStream log
FINEST: 250-STARTTLS
250-PIPELINING
Oct 18, 2013 11:59:25 PM com.sun.mail.util.LogOutputStream log
FINEST: 250-PIPELINING
250 8BITMIME
Oct 18, 2013 11:59:25 PM com.sun.mail.util.LogOutputStream log
FINEST: 250 8BITMIME
DEBUG SMTP: Found extension "AUTH=LOGIN", arg "CRAM-MD5 PLAIN"
Oct 18, 2013 11:59:25 PM com.sun.mail.smtp.SMTPTransport ehlo
FINE: Found extension "AUTH=LOGIN", arg "CRAM-MD5 PLAIN"
DEBUG SMTP: Found extension "AUTH", arg "LOGIN CRAM-MD5 PLAIN"
Oct 18, 2013 11:59:25 PM com.sun.mail.smtp.SMTPTransport ehlo
FINE: Found extension "AUTH", arg "LOGIN CRAM-MD5 PLAIN"
DEBUG SMTP: Found extension "STARTTLS", arg ""
Oct 18, 2013 11:59:25 PM com.sun.mail.smtp.SMTPTransport ehlo
FINE: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "PIPELINING", arg ""
Oct 18, 2013 11:59:25 PM com.sun.mail.smtp.SMTPTransport ehlo
FINE: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "8BITMIME", arg ""
Oct 18, 2013 11:59:25 PM com.sun.mail.smtp.SMTPTransport ehlo
FINE: Found extension "8BITMIME", arg ""
DEBUG SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM
Oct 18, 2013 11:59:25 PM com.sun.mail.smtp.SMTPTransport authenticate
FINE: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM
DEBUG SMTP: AUTH LOGIN command trace suppressed
Oct 18, 2013 11:59:25 PM com.sun.mail.smtp.SMTPTransport$Authenticator authenticate
FINE: AUTH LOGIN command trace suppressed
DEBUG SMTP: EOF: [EOF]
Oct 18, 2013 11:59:26 PM com.sun.mail.smtp.SMTPTransport readServerResponse
FINE: EOF: [EOF]
DEBUG SMTP: AUTH LOGIN failed
Oct 18, 2013 11:59:26 PM com.sun.mail.smtp.SMTPTransport$Authenticator authenticate
FINE: AUTH LOGIN failed
javax.mail.AuthenticationFailedException: [EOF]
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:295)
at com.compressus.medx.domain.service.AlertDomainService.sendEmailNotification(AlertDomainService.java:324)
at com.compressus.medx.domain.service.AlertDomainService.sendEmailNotification(AlertDomainService.java:224)
at com.compressus.medx.domain.service.UserDomainService.sendEmailNotificationByRole(UserDomainService.java:377)
at com.compressus.medx.domain.service.MailTest.main(MailTest.java:38)
[ERROR] Bad credentials or smtp server information supplied. Check configuration. [Ljava.lang.StackTraceElement;@394b394b
代码
请注意,即使 mail.smtp.auth 默认为 false,我尝试明确声明它,但仍然没有帮助。
MailServer mailServer = MailServer.getInstance();
if (mailServer != null)
{
Properties mailProps = new Properties();
mailProps.put("mail.smtp.host", mailServer.getSmtpServer());
mailProps.put("mail.smtp.port", mailServer.getSmtpPort());
//mailProps.put("mail.smtp.auth.login.disable", "true");
//mailProps.put("mail.smtp.auth", "false");
mailProps.put("mail.debug", "true");
/*
* Authentication is true if the email account name is set.
*/
if (mailServer.getSmtpAccount() != null && !mailServer.getSmtpAccount().equals(""))
{
mailProps.put("mail.smtp.auth", "true");
//mailProps.put("mail.smtp.auth.login.disable", "false");
mailProps.put("mail.smtp.auth.user", mailServer.getSmtpAccount());
mailProps.put("mail.smtp.auth.password", mailServer.getSmtpPassword());
}
if (mailServer.getSmtpEncryptionType().equals("TLS"))
{
mailProps.put("mail.smtp.starttls.enable", "true");
}
else if (mailServer.getSmtpEncryptionType().equals("SSL"))
{
mailProps.put("mail.smtp.socketFactory.port", mailServer.getSmtpPort());
mailProps.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
}
try
{
Session session = Session.getInstance(mailProps);
// create a message
Message msg = new MimeMessage(session);
// msg.setFrom(new InternetAddress(from));
// log.debug("setting from ... success:" + from);
InternetAddress[] addresses = null;
String[] addressList = toUserEmailAddress.split(",");
addresses = new InternetAddress[addressList.length];
for (int i = 0; i < addressList.length; i++)
addresses[i] = new InternetAddress(addressList[i]);
msg.setRecipients(Message.RecipientType.BCC, addresses);
msg.setSubject(subjectText);
msg.setSentDate(new java.util.Date());
if (from == null || from.trim().equals(""))
{
msg.setFrom(new InternetAddress("no-reply@compressus.com", "MEDxConnect"));
}
else
{
msg.setFrom(new InternetAddress("no-reply@compressus.com", from));
}
Multipart mainPart = new MimeMultipart();
// Add body to the email
BodyPart bodyTextPart = new MimeBodyPart();
bodyTextPart.setContent(bodyText, "text/html");
mainPart.addBodyPart(bodyTextPart);
// add attachment to the email
if (attachmentPath != null && !attachmentPath.isEmpty())
{
BodyPart attachPart = new MimeBodyPart();
FileDataSource fds = new FileDataSource(attachmentPath);
attachPart.setDataHandler(new DataHandler(fds));
attachPart.setFileName(fds.getName());
attachPart.setHeader("Content-Type", "application/pdf");
attachPart.setHeader("Content-Transfer-Encoding", "BASE64");
mainPart.addBodyPart(attachPart);
}
msg.setContent(mainPart);
Transport transport;
if (!mailServer.getSmtpEncryptionType().equals("None"))
transport = session.getTransport("smtps");
else
transport = session.getTransport("smtp");
transport.connect(mailServer.getSmtpServer(), mailServer.getSmtpPort(), mailServer.getSmtpAccount(), mailServer.getSmtpPassword());
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
log.info("Mail " + subjectText + " sent to " + toUserEmailAddress);
retValue = true;
}
catch (AuthenticationFailedException authEx)
{
java.util.logging.Logger l = java.util.logging.Logger.getLogger(AlertDomainService.class.getName());
l.setLevel(Level.FINEST);
log.error("Bad credentials or smtp server information supplied. Check configuration. " + authEx.getStackTrace());
}
catch (Exception ex)
{
log.error("Error occurred while sending email: " + ex);
do
{
if (ex instanceof SendFailedException)
{
String errormsg = "";
SendFailedException sfex = (SendFailedException) ex;
Address[] invalid = sfex.getInvalidAddresses();
if (invalid != null && invalid.length > 0)
{
errormsg = "Invalid Addresses: ";
for (int i = 0; i < invalid.length; i++)
{
errormsg += invalid[i] + " ";
}
log.error(errormsg);
}
Address[] validUnsent = sfex.getValidUnsentAddresses();
if (validUnsent != null && validUnsent.length > 0)
{
errormsg = "ValidUnsent Addresses: ";
for (int i = 0; i < validUnsent.length; i++)
{
errormsg += validUnsent[i] + " ";
}
log.error(errormsg);
}
Address[] validSent = sfex.getValidSentAddresses();
if (validSent != null && validSent.length > 0)
{
errormsg = "ValidSent Addresses: ";
for (int i = 0; i < validSent.length; i++)
{
errormsg += validSent[i] + " ";
}
log.error(errormsg);
}
}
if (ex instanceof MessagingException)
{
ex = ((MessagingException) ex).getNextException();
}
else
{
ex = null;
}
} while (ex != null);
}
}
else
{
log.error("No mail server information found. Email notification aborted");
}