1. 程式人生 > >javamail "535 5.7.3 Authentication unsuccessful"

javamail "535 5.7.3 Authentication unsuccessful"

JavaMailSenderImpl認證異常了,出現:javax.mail.AuthenticationFailedException: failed to connect的異常,
將JAVAMAIL的DEBUG日誌開啟,通過properties.setProperty("mail.debug", "true");可以看到出現了535 5.7.3 Authentication unsuccessful的問題,需要增加以下設定:
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.auth.mechanisms", "NTLM");
就可以解決了,如果非WINDOWS作業系統,需要將NTLM修改為digest方式;
具體程式碼如下:
JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
Properties properties = new Properties();
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.auth.mechanisms", "NTLM");
//properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
properties.setProperty("mail.debug", "true");
senderImpl.setJavaMailProperties(properties);
senderImpl.setHost("IP地址");
senderImpl.setUsername("認證使用者郵箱");
senderImpl.setPassword("對應的郵箱密碼");
//senderImpl.setPort(25);

MimeMessage mailMessage = senderImpl.createMimeMessage();
MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage,
true, "GBK");